@dudousxd/nestjs-agent-rag 0.13.1 → 0.14.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.
@@ -0,0 +1,174 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/nestjs/index.ts
22
+ var nestjs_exports = {};
23
+ __export(nestjs_exports, {
24
+ RAG_INDEX_DEPS: () => RAG_INDEX_DEPS,
25
+ RagModule: () => RagModule,
26
+ RagRepository: () => RagRepository
27
+ });
28
+ module.exports = __toCommonJS(nestjs_exports);
29
+ var import_nestjs_agent_rag = require("@dudousxd/nestjs-agent-rag");
30
+ var import_common = require("@nestjs/common");
31
+ function _ts_decorate(decorators, target, key, desc) {
32
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
33
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
34
+ r = Reflect.decorate(decorators, target, key, desc);
35
+ } else {
36
+ for (var i = decorators.length - 1; i >= 0; i--) {
37
+ if (d = decorators[i]) {
38
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
39
+ }
40
+ }
41
+ }
42
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
43
+ }
44
+ __name(_ts_decorate, "_ts_decorate");
45
+ function _ts_metadata(metadataKey, metadataValue) {
46
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
47
+ return Reflect.metadata(metadataKey, metadataValue);
48
+ }
49
+ }
50
+ __name(_ts_metadata, "_ts_metadata");
51
+ var RAG_INDEX_DEPS = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent-rag/index-deps");
52
+ var RagRepository = class {
53
+ static {
54
+ __name(this, "RagRepository");
55
+ }
56
+ ragIndexDeps;
57
+ chunk;
58
+ maxChunks;
59
+ minScore;
60
+ source(_row) {
61
+ return void 0;
62
+ }
63
+ metadata(_row) {
64
+ return {};
65
+ }
66
+ core;
67
+ // Built on first use: `name` and `scope` are subclass fields, and the deps are injected after both.
68
+ get ragIndex() {
69
+ this.core ??= new import_nestjs_agent_rag.RagIndex({
70
+ name: this.name,
71
+ scope: this.scope,
72
+ id: /* @__PURE__ */ __name((row) => this.id(row), "id"),
73
+ text: /* @__PURE__ */ __name((row) => this.text(row), "text"),
74
+ source: /* @__PURE__ */ __name((row) => this.source(row), "source"),
75
+ metadata: /* @__PURE__ */ __name((row) => this.metadata(row), "metadata"),
76
+ ...this.chunk !== void 0 ? {
77
+ chunk: this.chunk
78
+ } : {},
79
+ ...this.maxChunks !== void 0 ? {
80
+ maxChunks: this.maxChunks
81
+ } : {},
82
+ ...this.minScore !== void 0 ? {
83
+ minScore: this.minScore
84
+ } : {}
85
+ }, this.ragIndexDeps);
86
+ return this.core;
87
+ }
88
+ search(query, options) {
89
+ return this.ragIndex.search(query, options);
90
+ }
91
+ scoped(scope) {
92
+ return this.ragIndex.scoped(scope);
93
+ }
94
+ index(row) {
95
+ return this.ragIndex.index(row);
96
+ }
97
+ remove(rowOrId) {
98
+ return this.ragIndex.remove(rowOrId);
99
+ }
100
+ reconcile(liveIds, options) {
101
+ return this.ragIndex.reconcile(liveIds, options);
102
+ }
103
+ tool(options) {
104
+ return this.ragIndex.tool(options);
105
+ }
106
+ };
107
+ _ts_decorate([
108
+ (0, import_common.Inject)(RAG_INDEX_DEPS),
109
+ _ts_metadata("design:type", typeof RagIndexDeps === "undefined" ? Object : RagIndexDeps)
110
+ ], RagRepository.prototype, "ragIndexDeps", void 0);
111
+ var RagFeatureModule = class RagFeatureModule2 {
112
+ static {
113
+ __name(this, "RagFeatureModule");
114
+ }
115
+ };
116
+ RagFeatureModule = _ts_decorate([
117
+ (0, import_common.Module)({})
118
+ ], RagFeatureModule);
119
+ var RagModule = class _RagModule {
120
+ static {
121
+ __name(this, "RagModule");
122
+ }
123
+ static forRoot(deps) {
124
+ return {
125
+ module: _RagModule,
126
+ global: true,
127
+ providers: [
128
+ {
129
+ provide: RAG_INDEX_DEPS,
130
+ useValue: deps
131
+ }
132
+ ],
133
+ exports: [
134
+ RAG_INDEX_DEPS
135
+ ]
136
+ };
137
+ }
138
+ static forRootAsync(options) {
139
+ return {
140
+ module: _RagModule,
141
+ global: true,
142
+ ...options.imports !== void 0 ? {
143
+ imports: options.imports
144
+ } : {},
145
+ providers: [
146
+ {
147
+ provide: RAG_INDEX_DEPS,
148
+ inject: options.inject ?? [],
149
+ useFactory: options.useFactory
150
+ }
151
+ ],
152
+ exports: [
153
+ RAG_INDEX_DEPS
154
+ ]
155
+ };
156
+ }
157
+ static forFeature(repositories) {
158
+ return {
159
+ module: RagFeatureModule,
160
+ providers: repositories,
161
+ exports: repositories
162
+ };
163
+ }
164
+ };
165
+ RagModule = _ts_decorate([
166
+ (0, import_common.Module)({})
167
+ ], RagModule);
168
+ // Annotate the CommonJS export names for ESM import in node:
169
+ 0 && (module.exports = {
170
+ RAG_INDEX_DEPS,
171
+ RagModule,
172
+ RagRepository
173
+ });
174
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/nestjs/index.ts"],"sourcesContent":["import type { Passage, Retriever } from '@dudousxd/nestjs-agent-core';\nimport {\n type ChunkOptions,\n RagIndex,\n type RagIndexDeps,\n type RagReconcileOptions,\n type RagReconcileResult,\n type RagScopeDefinition,\n type RagScopeOf,\n type RagScopeValue,\n type RagSearchOptions,\n type RagToolOptions,\n type RetrievalTool,\n} from '@dudousxd/nestjs-agent-rag';\nimport { type DynamicModule, Inject, Module, type Type } from '@nestjs/common';\n\n/** Token of the `store` and `embedder` every {@link RagRepository} uses. */\nexport const RAG_INDEX_DEPS = Symbol.for('@dudousxd/nestjs-agent-rag/index-deps');\n\n/**\n * RAG for one model, as a provider of your own. The entity (or Drizzle table) doesn't change:\n *\n * ```ts\n * @Injectable()\n * export class DocumentsRag extends RagRepository<ProjectDocument> {\n * protected readonly name = 'project_documents';\n * readonly scope = { projectId: (d: ProjectDocument) => d.project.id };\n * protected id(d: ProjectDocument) { return d.id; }\n * protected text(d: ProjectDocument) { return d.textContent; }\n * }\n *\n * await documentsRag.index(doc);\n * await documentsRag.search('question', { scope: { projectId } });\n * ```\n *\n * `scope` is abstract, so leaving it out doesn't compile, and its shape types `search`. A corpus\n * shared by everyone has to say `scope = {}`.\n */\nexport abstract class RagRepository<Row> {\n @Inject(RAG_INDEX_DEPS) private readonly ragIndexDeps!: RagIndexDeps;\n\n /** Namespaces document ids and the metadata that separates models sharing a store. */\n protected abstract readonly name: string;\n abstract readonly scope: RagScopeDefinition<Row>;\n protected readonly chunk?: ChunkOptions;\n protected readonly maxChunks?: number;\n protected readonly minScore?: number;\n\n /** The value `remove` needs once the row is gone. */\n abstract id(row: Row): RagScopeValue;\n protected abstract text(row: Row): string | null | undefined;\n protected source(_row: Row): string | null | undefined {\n return undefined;\n }\n protected metadata(_row: Row): Record<string, unknown> {\n return {};\n }\n\n private core?: RagIndex<Row, RagScopeDefinition<Row>>;\n\n // Built on first use: `name` and `scope` are subclass fields, and the deps are injected after both.\n private get ragIndex(): RagIndex<Row, RagScopeDefinition<Row>> {\n this.core ??= new RagIndex(\n {\n name: this.name,\n scope: this.scope,\n id: (row) => this.id(row),\n text: (row) => this.text(row),\n source: (row) => this.source(row),\n metadata: (row) => this.metadata(row),\n ...(this.chunk !== undefined ? { chunk: this.chunk } : {}),\n ...(this.maxChunks !== undefined ? { maxChunks: this.maxChunks } : {}),\n ...(this.minScore !== undefined ? { minScore: this.minScore } : {}),\n },\n this.ragIndexDeps,\n );\n return this.core;\n }\n\n search(query: string, options: RagSearchOptions<this['scope']>): Promise<Passage[]> {\n return this.ragIndex.search(query, options as never);\n }\n\n scoped(scope: RagScopeOf<this['scope']>): Retriever {\n return this.ragIndex.scoped(scope as never);\n }\n\n index(row: Row): Promise<number> {\n return this.ragIndex.index(row);\n }\n\n remove(rowOrId: Row | RagScopeValue): Promise<void> {\n return this.ragIndex.remove(rowOrId);\n }\n\n reconcile(\n liveIds: Iterable<RagScopeValue> | AsyncIterable<RagScopeValue>,\n options?: RagReconcileOptions<this['scope']>,\n ): Promise<RagReconcileResult> {\n return this.ragIndex.reconcile(liveIds, options as never);\n }\n\n tool(options: RagToolOptions<this['scope']>): RetrievalTool {\n return this.ragIndex.tool(options as never);\n }\n}\n\nexport interface RagModuleAsyncOptions {\n imports?: DynamicModule['imports'];\n inject?: unknown[];\n useFactory: (...args: never[]) => RagIndexDeps | Promise<RagIndexDeps>;\n}\n\n@Module({})\nclass RagFeatureModule {}\n\n/** `forRoot` holds the store and embedder, globally; `forFeature` registers repositories. */\n@Module({})\nexport class RagModule {\n static forRoot(deps: RagIndexDeps): DynamicModule {\n return {\n module: RagModule,\n global: true,\n providers: [{ provide: RAG_INDEX_DEPS, useValue: deps }],\n exports: [RAG_INDEX_DEPS],\n };\n }\n\n static forRootAsync(options: RagModuleAsyncOptions): DynamicModule {\n return {\n module: RagModule,\n global: true,\n ...(options.imports !== undefined ? { imports: options.imports } : {}),\n providers: [\n {\n provide: RAG_INDEX_DEPS,\n inject: (options.inject ?? []) as never[],\n useFactory: options.useFactory as (...args: unknown[]) => RagIndexDeps,\n },\n ],\n exports: [RAG_INDEX_DEPS],\n };\n }\n\n static forFeature(repositories: Type<unknown>[]): DynamicModule {\n return { module: RagFeatureModule, providers: repositories, exports: repositories };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,8BAYO;AACP,oBAA8D;;;;;;;;;;;;;;;;;;;;;AAGvD,IAAMA,iBAAiBC,uBAAOC,IAAI,uCAAA;AAqBlC,IAAeC,gBAAf,MAAeA;SAAAA;;;EACqBC;EAKtBC;EACAC;EACAC;EAKTC,OAAOC,MAAsC;AACrD,WAAOC;EACT;EACUC,SAASF,MAAoC;AACrD,WAAO,CAAC;EACV;EAEQG;;EAGR,IAAYC,WAAmD;AAC7D,SAAKD,SAAS,IAAIE,iCAChB;MACEC,MAAM,KAAKA;MACXC,OAAO,KAAKA;MACZC,IAAI,wBAACC,QAAQ,KAAKD,GAAGC,GAAAA,GAAjB;MACJC,MAAM,wBAACD,QAAQ,KAAKC,KAAKD,GAAAA,GAAnB;MACNV,QAAQ,wBAACU,QAAQ,KAAKV,OAAOU,GAAAA,GAArB;MACRP,UAAU,wBAACO,QAAQ,KAAKP,SAASO,GAAAA,GAAvB;MACV,GAAI,KAAKb,UAAUK,SAAY;QAAEL,OAAO,KAAKA;MAAM,IAAI,CAAC;MACxD,GAAI,KAAKC,cAAcI,SAAY;QAAEJ,WAAW,KAAKA;MAAU,IAAI,CAAC;MACpE,GAAI,KAAKC,aAAaG,SAAY;QAAEH,UAAU,KAAKA;MAAS,IAAI,CAAC;IACnE,GACA,KAAKH,YAAY;AAEnB,WAAO,KAAKQ;EACd;EAEAQ,OAAOC,OAAeC,SAA8D;AAClF,WAAO,KAAKT,SAASO,OAAOC,OAAOC,OAAAA;EACrC;EAEAC,OAAOP,OAA6C;AAClD,WAAO,KAAKH,SAASU,OAAOP,KAAAA;EAC9B;EAEAQ,MAAMN,KAA2B;AAC/B,WAAO,KAAKL,SAASW,MAAMN,GAAAA;EAC7B;EAEAO,OAAOC,SAA6C;AAClD,WAAO,KAAKb,SAASY,OAAOC,OAAAA;EAC9B;EAEAC,UACEC,SACAN,SAC6B;AAC7B,WAAO,KAAKT,SAASc,UAAUC,SAASN,OAAAA;EAC1C;EAEAO,KAAKP,SAAuD;AAC1D,WAAO,KAAKT,SAASgB,KAAKP,OAAAA;EAC5B;AACF;;;;;AAQA,IACMQ,mBADN,MACMA,kBAAAA;SAAAA;;;AAAkB;;;;AAIjB,IAAMC,YAAN,MAAMA,WAAAA;SAAAA;;;EACX,OAAOC,QAAQC,MAAmC;AAChD,WAAO;MACLC,QAAQH;MACRI,QAAQ;MACRC,WAAW;QAAC;UAAEC,SAASrC;UAAgBsC,UAAUL;QAAK;;MACtDM,SAAS;QAACvC;;IACZ;EACF;EAEA,OAAOwC,aAAalB,SAA+C;AACjE,WAAO;MACLY,QAAQH;MACRI,QAAQ;MACR,GAAIb,QAAQmB,YAAY/B,SAAY;QAAE+B,SAASnB,QAAQmB;MAAQ,IAAI,CAAC;MACpEL,WAAW;QACT;UACEC,SAASrC;UACT0C,QAASpB,QAAQoB,UAAU,CAAA;UAC3BC,YAAYrB,QAAQqB;QACtB;;MAEFJ,SAAS;QAACvC;;IACZ;EACF;EAEA,OAAO4C,WAAWC,cAA8C;AAC9D,WAAO;MAAEX,QAAQJ;MAAkBM,WAAWS;MAAcN,SAASM;IAAa;EACpF;AACF;;;;","names":["RAG_INDEX_DEPS","Symbol","for","RagRepository","ragIndexDeps","chunk","maxChunks","minScore","source","_row","undefined","metadata","core","ragIndex","RagIndex","name","scope","id","row","text","search","query","options","scoped","index","remove","rowOrId","reconcile","liveIds","tool","RagFeatureModule","RagModule","forRoot","deps","module","global","providers","provide","useValue","exports","forRootAsync","imports","inject","useFactory","forFeature","repositories"]}
@@ -0,0 +1,60 @@
1
+ import { Passage, Retriever } from '@dudousxd/nestjs-agent-core';
2
+ import { RagIndexDeps, RagScopeDefinition, ChunkOptions, RagScopeValue, RagSearchOptions, RagScopeOf, RagReconcileOptions, RagReconcileResult, RagToolOptions, RetrievalTool } from '@dudousxd/nestjs-agent-rag';
3
+ import { DynamicModule, Type } from '@nestjs/common';
4
+
5
+ /** Token of the `store` and `embedder` every {@link RagRepository} uses. */
6
+ declare const RAG_INDEX_DEPS: unique symbol;
7
+ /**
8
+ * RAG for one model, as a provider of your own. The entity (or Drizzle table) doesn't change:
9
+ *
10
+ * ```ts
11
+ * @Injectable()
12
+ * export class DocumentsRag extends RagRepository<ProjectDocument> {
13
+ * protected readonly name = 'project_documents';
14
+ * readonly scope = { projectId: (d: ProjectDocument) => d.project.id };
15
+ * protected id(d: ProjectDocument) { return d.id; }
16
+ * protected text(d: ProjectDocument) { return d.textContent; }
17
+ * }
18
+ *
19
+ * await documentsRag.index(doc);
20
+ * await documentsRag.search('question', { scope: { projectId } });
21
+ * ```
22
+ *
23
+ * `scope` is abstract, so leaving it out doesn't compile, and its shape types `search`. A corpus
24
+ * shared by everyone has to say `scope = {}`.
25
+ */
26
+ declare abstract class RagRepository<Row> {
27
+ private readonly ragIndexDeps;
28
+ /** Namespaces document ids and the metadata that separates models sharing a store. */
29
+ protected abstract readonly name: string;
30
+ abstract readonly scope: RagScopeDefinition<Row>;
31
+ protected readonly chunk?: ChunkOptions;
32
+ protected readonly maxChunks?: number;
33
+ protected readonly minScore?: number;
34
+ /** The value `remove` needs once the row is gone. */
35
+ abstract id(row: Row): RagScopeValue;
36
+ protected abstract text(row: Row): string | null | undefined;
37
+ protected source(_row: Row): string | null | undefined;
38
+ protected metadata(_row: Row): Record<string, unknown>;
39
+ private core?;
40
+ private get ragIndex();
41
+ search(query: string, options: RagSearchOptions<this['scope']>): Promise<Passage[]>;
42
+ scoped(scope: RagScopeOf<this['scope']>): Retriever;
43
+ index(row: Row): Promise<number>;
44
+ remove(rowOrId: Row | RagScopeValue): Promise<void>;
45
+ reconcile(liveIds: Iterable<RagScopeValue> | AsyncIterable<RagScopeValue>, options?: RagReconcileOptions<this['scope']>): Promise<RagReconcileResult>;
46
+ tool(options: RagToolOptions<this['scope']>): RetrievalTool;
47
+ }
48
+ interface RagModuleAsyncOptions {
49
+ imports?: DynamicModule['imports'];
50
+ inject?: unknown[];
51
+ useFactory: (...args: never[]) => RagIndexDeps | Promise<RagIndexDeps>;
52
+ }
53
+ /** `forRoot` holds the store and embedder, globally; `forFeature` registers repositories. */
54
+ declare class RagModule {
55
+ static forRoot(deps: RagIndexDeps): DynamicModule;
56
+ static forRootAsync(options: RagModuleAsyncOptions): DynamicModule;
57
+ static forFeature(repositories: Type<unknown>[]): DynamicModule;
58
+ }
59
+
60
+ export { RAG_INDEX_DEPS, RagModule, type RagModuleAsyncOptions, RagRepository };
@@ -0,0 +1,60 @@
1
+ import { Passage, Retriever } from '@dudousxd/nestjs-agent-core';
2
+ import { RagIndexDeps, RagScopeDefinition, ChunkOptions, RagScopeValue, RagSearchOptions, RagScopeOf, RagReconcileOptions, RagReconcileResult, RagToolOptions, RetrievalTool } from '@dudousxd/nestjs-agent-rag';
3
+ import { DynamicModule, Type } from '@nestjs/common';
4
+
5
+ /** Token of the `store` and `embedder` every {@link RagRepository} uses. */
6
+ declare const RAG_INDEX_DEPS: unique symbol;
7
+ /**
8
+ * RAG for one model, as a provider of your own. The entity (or Drizzle table) doesn't change:
9
+ *
10
+ * ```ts
11
+ * @Injectable()
12
+ * export class DocumentsRag extends RagRepository<ProjectDocument> {
13
+ * protected readonly name = 'project_documents';
14
+ * readonly scope = { projectId: (d: ProjectDocument) => d.project.id };
15
+ * protected id(d: ProjectDocument) { return d.id; }
16
+ * protected text(d: ProjectDocument) { return d.textContent; }
17
+ * }
18
+ *
19
+ * await documentsRag.index(doc);
20
+ * await documentsRag.search('question', { scope: { projectId } });
21
+ * ```
22
+ *
23
+ * `scope` is abstract, so leaving it out doesn't compile, and its shape types `search`. A corpus
24
+ * shared by everyone has to say `scope = {}`.
25
+ */
26
+ declare abstract class RagRepository<Row> {
27
+ private readonly ragIndexDeps;
28
+ /** Namespaces document ids and the metadata that separates models sharing a store. */
29
+ protected abstract readonly name: string;
30
+ abstract readonly scope: RagScopeDefinition<Row>;
31
+ protected readonly chunk?: ChunkOptions;
32
+ protected readonly maxChunks?: number;
33
+ protected readonly minScore?: number;
34
+ /** The value `remove` needs once the row is gone. */
35
+ abstract id(row: Row): RagScopeValue;
36
+ protected abstract text(row: Row): string | null | undefined;
37
+ protected source(_row: Row): string | null | undefined;
38
+ protected metadata(_row: Row): Record<string, unknown>;
39
+ private core?;
40
+ private get ragIndex();
41
+ search(query: string, options: RagSearchOptions<this['scope']>): Promise<Passage[]>;
42
+ scoped(scope: RagScopeOf<this['scope']>): Retriever;
43
+ index(row: Row): Promise<number>;
44
+ remove(rowOrId: Row | RagScopeValue): Promise<void>;
45
+ reconcile(liveIds: Iterable<RagScopeValue> | AsyncIterable<RagScopeValue>, options?: RagReconcileOptions<this['scope']>): Promise<RagReconcileResult>;
46
+ tool(options: RagToolOptions<this['scope']>): RetrievalTool;
47
+ }
48
+ interface RagModuleAsyncOptions {
49
+ imports?: DynamicModule['imports'];
50
+ inject?: unknown[];
51
+ useFactory: (...args: never[]) => RagIndexDeps | Promise<RagIndexDeps>;
52
+ }
53
+ /** `forRoot` holds the store and embedder, globally; `forFeature` registers repositories. */
54
+ declare class RagModule {
55
+ static forRoot(deps: RagIndexDeps): DynamicModule;
56
+ static forRootAsync(options: RagModuleAsyncOptions): DynamicModule;
57
+ static forFeature(repositories: Type<unknown>[]): DynamicModule;
58
+ }
59
+
60
+ export { RAG_INDEX_DEPS, RagModule, type RagModuleAsyncOptions, RagRepository };
@@ -0,0 +1,149 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/nestjs/index.ts
5
+ import { RagIndex } from "@dudousxd/nestjs-agent-rag";
6
+ import { Inject, Module } from "@nestjs/common";
7
+ function _ts_decorate(decorators, target, key, desc) {
8
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
10
+ r = Reflect.decorate(decorators, target, key, desc);
11
+ } else {
12
+ for (var i = decorators.length - 1; i >= 0; i--) {
13
+ if (d = decorators[i]) {
14
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
15
+ }
16
+ }
17
+ }
18
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
19
+ }
20
+ __name(_ts_decorate, "_ts_decorate");
21
+ function _ts_metadata(metadataKey, metadataValue) {
22
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
23
+ return Reflect.metadata(metadataKey, metadataValue);
24
+ }
25
+ }
26
+ __name(_ts_metadata, "_ts_metadata");
27
+ var RAG_INDEX_DEPS = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent-rag/index-deps");
28
+ var RagRepository = class {
29
+ static {
30
+ __name(this, "RagRepository");
31
+ }
32
+ ragIndexDeps;
33
+ chunk;
34
+ maxChunks;
35
+ minScore;
36
+ source(_row) {
37
+ return void 0;
38
+ }
39
+ metadata(_row) {
40
+ return {};
41
+ }
42
+ core;
43
+ // Built on first use: `name` and `scope` are subclass fields, and the deps are injected after both.
44
+ get ragIndex() {
45
+ this.core ??= new RagIndex({
46
+ name: this.name,
47
+ scope: this.scope,
48
+ id: /* @__PURE__ */ __name((row) => this.id(row), "id"),
49
+ text: /* @__PURE__ */ __name((row) => this.text(row), "text"),
50
+ source: /* @__PURE__ */ __name((row) => this.source(row), "source"),
51
+ metadata: /* @__PURE__ */ __name((row) => this.metadata(row), "metadata"),
52
+ ...this.chunk !== void 0 ? {
53
+ chunk: this.chunk
54
+ } : {},
55
+ ...this.maxChunks !== void 0 ? {
56
+ maxChunks: this.maxChunks
57
+ } : {},
58
+ ...this.minScore !== void 0 ? {
59
+ minScore: this.minScore
60
+ } : {}
61
+ }, this.ragIndexDeps);
62
+ return this.core;
63
+ }
64
+ search(query, options) {
65
+ return this.ragIndex.search(query, options);
66
+ }
67
+ scoped(scope) {
68
+ return this.ragIndex.scoped(scope);
69
+ }
70
+ index(row) {
71
+ return this.ragIndex.index(row);
72
+ }
73
+ remove(rowOrId) {
74
+ return this.ragIndex.remove(rowOrId);
75
+ }
76
+ reconcile(liveIds, options) {
77
+ return this.ragIndex.reconcile(liveIds, options);
78
+ }
79
+ tool(options) {
80
+ return this.ragIndex.tool(options);
81
+ }
82
+ };
83
+ _ts_decorate([
84
+ Inject(RAG_INDEX_DEPS),
85
+ _ts_metadata("design:type", typeof RagIndexDeps === "undefined" ? Object : RagIndexDeps)
86
+ ], RagRepository.prototype, "ragIndexDeps", void 0);
87
+ var RagFeatureModule = class RagFeatureModule2 {
88
+ static {
89
+ __name(this, "RagFeatureModule");
90
+ }
91
+ };
92
+ RagFeatureModule = _ts_decorate([
93
+ Module({})
94
+ ], RagFeatureModule);
95
+ var RagModule = class _RagModule {
96
+ static {
97
+ __name(this, "RagModule");
98
+ }
99
+ static forRoot(deps) {
100
+ return {
101
+ module: _RagModule,
102
+ global: true,
103
+ providers: [
104
+ {
105
+ provide: RAG_INDEX_DEPS,
106
+ useValue: deps
107
+ }
108
+ ],
109
+ exports: [
110
+ RAG_INDEX_DEPS
111
+ ]
112
+ };
113
+ }
114
+ static forRootAsync(options) {
115
+ return {
116
+ module: _RagModule,
117
+ global: true,
118
+ ...options.imports !== void 0 ? {
119
+ imports: options.imports
120
+ } : {},
121
+ providers: [
122
+ {
123
+ provide: RAG_INDEX_DEPS,
124
+ inject: options.inject ?? [],
125
+ useFactory: options.useFactory
126
+ }
127
+ ],
128
+ exports: [
129
+ RAG_INDEX_DEPS
130
+ ]
131
+ };
132
+ }
133
+ static forFeature(repositories) {
134
+ return {
135
+ module: RagFeatureModule,
136
+ providers: repositories,
137
+ exports: repositories
138
+ };
139
+ }
140
+ };
141
+ RagModule = _ts_decorate([
142
+ Module({})
143
+ ], RagModule);
144
+ export {
145
+ RAG_INDEX_DEPS,
146
+ RagModule,
147
+ RagRepository
148
+ };
149
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/nestjs/index.ts"],"sourcesContent":["import type { Passage, Retriever } from '@dudousxd/nestjs-agent-core';\nimport {\n type ChunkOptions,\n RagIndex,\n type RagIndexDeps,\n type RagReconcileOptions,\n type RagReconcileResult,\n type RagScopeDefinition,\n type RagScopeOf,\n type RagScopeValue,\n type RagSearchOptions,\n type RagToolOptions,\n type RetrievalTool,\n} from '@dudousxd/nestjs-agent-rag';\nimport { type DynamicModule, Inject, Module, type Type } from '@nestjs/common';\n\n/** Token of the `store` and `embedder` every {@link RagRepository} uses. */\nexport const RAG_INDEX_DEPS = Symbol.for('@dudousxd/nestjs-agent-rag/index-deps');\n\n/**\n * RAG for one model, as a provider of your own. The entity (or Drizzle table) doesn't change:\n *\n * ```ts\n * @Injectable()\n * export class DocumentsRag extends RagRepository<ProjectDocument> {\n * protected readonly name = 'project_documents';\n * readonly scope = { projectId: (d: ProjectDocument) => d.project.id };\n * protected id(d: ProjectDocument) { return d.id; }\n * protected text(d: ProjectDocument) { return d.textContent; }\n * }\n *\n * await documentsRag.index(doc);\n * await documentsRag.search('question', { scope: { projectId } });\n * ```\n *\n * `scope` is abstract, so leaving it out doesn't compile, and its shape types `search`. A corpus\n * shared by everyone has to say `scope = {}`.\n */\nexport abstract class RagRepository<Row> {\n @Inject(RAG_INDEX_DEPS) private readonly ragIndexDeps!: RagIndexDeps;\n\n /** Namespaces document ids and the metadata that separates models sharing a store. */\n protected abstract readonly name: string;\n abstract readonly scope: RagScopeDefinition<Row>;\n protected readonly chunk?: ChunkOptions;\n protected readonly maxChunks?: number;\n protected readonly minScore?: number;\n\n /** The value `remove` needs once the row is gone. */\n abstract id(row: Row): RagScopeValue;\n protected abstract text(row: Row): string | null | undefined;\n protected source(_row: Row): string | null | undefined {\n return undefined;\n }\n protected metadata(_row: Row): Record<string, unknown> {\n return {};\n }\n\n private core?: RagIndex<Row, RagScopeDefinition<Row>>;\n\n // Built on first use: `name` and `scope` are subclass fields, and the deps are injected after both.\n private get ragIndex(): RagIndex<Row, RagScopeDefinition<Row>> {\n this.core ??= new RagIndex(\n {\n name: this.name,\n scope: this.scope,\n id: (row) => this.id(row),\n text: (row) => this.text(row),\n source: (row) => this.source(row),\n metadata: (row) => this.metadata(row),\n ...(this.chunk !== undefined ? { chunk: this.chunk } : {}),\n ...(this.maxChunks !== undefined ? { maxChunks: this.maxChunks } : {}),\n ...(this.minScore !== undefined ? { minScore: this.minScore } : {}),\n },\n this.ragIndexDeps,\n );\n return this.core;\n }\n\n search(query: string, options: RagSearchOptions<this['scope']>): Promise<Passage[]> {\n return this.ragIndex.search(query, options as never);\n }\n\n scoped(scope: RagScopeOf<this['scope']>): Retriever {\n return this.ragIndex.scoped(scope as never);\n }\n\n index(row: Row): Promise<number> {\n return this.ragIndex.index(row);\n }\n\n remove(rowOrId: Row | RagScopeValue): Promise<void> {\n return this.ragIndex.remove(rowOrId);\n }\n\n reconcile(\n liveIds: Iterable<RagScopeValue> | AsyncIterable<RagScopeValue>,\n options?: RagReconcileOptions<this['scope']>,\n ): Promise<RagReconcileResult> {\n return this.ragIndex.reconcile(liveIds, options as never);\n }\n\n tool(options: RagToolOptions<this['scope']>): RetrievalTool {\n return this.ragIndex.tool(options as never);\n }\n}\n\nexport interface RagModuleAsyncOptions {\n imports?: DynamicModule['imports'];\n inject?: unknown[];\n useFactory: (...args: never[]) => RagIndexDeps | Promise<RagIndexDeps>;\n}\n\n@Module({})\nclass RagFeatureModule {}\n\n/** `forRoot` holds the store and embedder, globally; `forFeature` registers repositories. */\n@Module({})\nexport class RagModule {\n static forRoot(deps: RagIndexDeps): DynamicModule {\n return {\n module: RagModule,\n global: true,\n providers: [{ provide: RAG_INDEX_DEPS, useValue: deps }],\n exports: [RAG_INDEX_DEPS],\n };\n }\n\n static forRootAsync(options: RagModuleAsyncOptions): DynamicModule {\n return {\n module: RagModule,\n global: true,\n ...(options.imports !== undefined ? { imports: options.imports } : {}),\n providers: [\n {\n provide: RAG_INDEX_DEPS,\n inject: (options.inject ?? []) as never[],\n useFactory: options.useFactory as (...args: unknown[]) => RagIndexDeps,\n },\n ],\n exports: [RAG_INDEX_DEPS],\n };\n }\n\n static forFeature(repositories: Type<unknown>[]): DynamicModule {\n return { module: RagFeatureModule, providers: repositories, exports: repositories };\n }\n}\n"],"mappings":";;;;AACA,SAEEA,gBAUK;AACP,SAA6BC,QAAQC,cAAyB;;;;;;;;;;;;;;;;;;;;;AAGvD,IAAMC,iBAAiBC,uBAAOC,IAAI,uCAAA;AAqBlC,IAAeC,gBAAf,MAAeA;SAAAA;;;EACqBC;EAKtBC;EACAC;EACAC;EAKTC,OAAOC,MAAsC;AACrD,WAAOC;EACT;EACUC,SAASF,MAAoC;AACrD,WAAO,CAAC;EACV;EAEQG;;EAGR,IAAYC,WAAmD;AAC7D,SAAKD,SAAS,IAAIE,SAChB;MACEC,MAAM,KAAKA;MACXC,OAAO,KAAKA;MACZC,IAAI,wBAACC,QAAQ,KAAKD,GAAGC,GAAAA,GAAjB;MACJC,MAAM,wBAACD,QAAQ,KAAKC,KAAKD,GAAAA,GAAnB;MACNV,QAAQ,wBAACU,QAAQ,KAAKV,OAAOU,GAAAA,GAArB;MACRP,UAAU,wBAACO,QAAQ,KAAKP,SAASO,GAAAA,GAAvB;MACV,GAAI,KAAKb,UAAUK,SAAY;QAAEL,OAAO,KAAKA;MAAM,IAAI,CAAC;MACxD,GAAI,KAAKC,cAAcI,SAAY;QAAEJ,WAAW,KAAKA;MAAU,IAAI,CAAC;MACpE,GAAI,KAAKC,aAAaG,SAAY;QAAEH,UAAU,KAAKA;MAAS,IAAI,CAAC;IACnE,GACA,KAAKH,YAAY;AAEnB,WAAO,KAAKQ;EACd;EAEAQ,OAAOC,OAAeC,SAA8D;AAClF,WAAO,KAAKT,SAASO,OAAOC,OAAOC,OAAAA;EACrC;EAEAC,OAAOP,OAA6C;AAClD,WAAO,KAAKH,SAASU,OAAOP,KAAAA;EAC9B;EAEAQ,MAAMN,KAA2B;AAC/B,WAAO,KAAKL,SAASW,MAAMN,GAAAA;EAC7B;EAEAO,OAAOC,SAA6C;AAClD,WAAO,KAAKb,SAASY,OAAOC,OAAAA;EAC9B;EAEAC,UACEC,SACAN,SAC6B;AAC7B,WAAO,KAAKT,SAASc,UAAUC,SAASN,OAAAA;EAC1C;EAEAO,KAAKP,SAAuD;AAC1D,WAAO,KAAKT,SAASgB,KAAKP,OAAAA;EAC5B;AACF;;;;;AAQA,IACMQ,mBADN,MACMA,kBAAAA;SAAAA;;;AAAkB;;;;AAIjB,IAAMC,YAAN,MAAMA,WAAAA;SAAAA;;;EACX,OAAOC,QAAQC,MAAmC;AAChD,WAAO;MACLC,QAAQH;MACRI,QAAQ;MACRC,WAAW;QAAC;UAAEC,SAASrC;UAAgBsC,UAAUL;QAAK;;MACtDM,SAAS;QAACvC;;IACZ;EACF;EAEA,OAAOwC,aAAalB,SAA+C;AACjE,WAAO;MACLY,QAAQH;MACRI,QAAQ;MACR,GAAIb,QAAQmB,YAAY/B,SAAY;QAAE+B,SAASnB,QAAQmB;MAAQ,IAAI,CAAC;MACpEL,WAAW;QACT;UACEC,SAASrC;UACT0C,QAASpB,QAAQoB,UAAU,CAAA;UAC3BC,YAAYrB,QAAQqB;QACtB;;MAEFJ,SAAS;QAACvC;;IACZ;EACF;EAEA,OAAO4C,WAAWC,cAA8C;AAC9D,WAAO;MAAEX,QAAQJ;MAAkBM,WAAWS;MAAcN,SAASM;IAAa;EACpF;AACF;;;;","names":["RagIndex","Inject","Module","RAG_INDEX_DEPS","Symbol","for","RagRepository","ragIndexDeps","chunk","maxChunks","minScore","source","_row","undefined","metadata","core","ragIndex","RagIndex","name","scope","id","row","text","search","query","options","scoped","index","remove","rowOrId","reconcile","liveIds","tool","RagFeatureModule","RagModule","forRoot","deps","module","global","providers","provide","useValue","exports","forRootAsync","imports","inject","useFactory","forFeature","repositories"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dudousxd/nestjs-agent-rag",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/DavideCarvalho/nestjs-agent.git",
@@ -32,6 +32,16 @@
32
32
  "types": "./dist/index.d.cts",
33
33
  "default": "./dist/index.cjs"
34
34
  }
35
+ },
36
+ "./nestjs": {
37
+ "import": {
38
+ "types": "./dist/nestjs/index.d.ts",
39
+ "default": "./dist/nestjs/index.js"
40
+ },
41
+ "require": {
42
+ "types": "./dist/nestjs/index.d.cts",
43
+ "default": "./dist/nestjs/index.cjs"
44
+ }
35
45
  }
36
46
  },
37
47
  "files": [
@@ -39,24 +49,34 @@
39
49
  "NOTICE"
40
50
  ],
41
51
  "peerDependencies": {
42
- "@dudousxd/nestjs-agent-core": ">=0.10.0 <1.0.0"
52
+ "@dudousxd/nestjs-agent-core": ">=0.10.0 <1.0.0",
53
+ "@nestjs/common": "^10.0.0 || ^11.0.0 || ^12.0.0"
43
54
  },
44
55
  "dependencies": {
45
56
  "@dudousxd/nestjs-diagnostics": "0.7.2",
46
57
  "@standard-schema/spec": "1.1.0"
47
58
  },
48
59
  "devDependencies": {
60
+ "@nestjs/common": "12.0.4",
61
+ "@nestjs/core": "12.0.4",
62
+ "@nestjs/testing": "12.0.4",
49
63
  "@testcontainers/postgresql": "12.2.0",
50
64
  "@types/pg": "8.23.1",
51
65
  "drizzle-orm": "0.45.3",
52
66
  "pg": "8.23.1",
53
67
  "redis": "6.2.1",
68
+ "reflect-metadata": "0.2.2",
54
69
  "testcontainers": "12.2.0",
55
70
  "tsup": "8.5.1",
56
71
  "typescript": "5.9.3",
57
72
  "vitest": "2.1.9",
58
- "@dudousxd/nestjs-agent-core": "0.41.0",
59
- "@dudousxd/nestjs-agent-testing": "0.26.1"
73
+ "@dudousxd/nestjs-agent-core": "0.61.0",
74
+ "@dudousxd/nestjs-agent-testing": "0.34.0"
75
+ },
76
+ "peerDependenciesMeta": {
77
+ "@nestjs/common": {
78
+ "optional": true
79
+ }
60
80
  },
61
81
  "scripts": {
62
82
  "build": "tsup",