@telygent/ai-sdk 0.1.16 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -3
- package/dist/adapters/mongo.js +205 -1
- package/dist/client.d.ts +3 -1
- package/dist/client.js +705 -1
- package/dist/example.js +21 -1
- package/dist/history.js +68 -1
- package/dist/http.d.ts +1 -0
- package/dist/http.js +105 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -1
- package/dist/middleware.js +11 -1
- package/dist/registry.js +25 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.js +2 -1
- package/dist/validation.js +249 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ npm install ioredis mongodb
|
|
|
20
20
|
import { createAiClient, createRedisHistoryStore, createMongoAdapter, type ModelRegistry } from "@telygent/ai-sdk";
|
|
21
21
|
import { MongoClient } from "mongodb";
|
|
22
22
|
|
|
23
|
+
// createRedisHistoryStore reuses a shared client per URL.
|
|
23
24
|
const historyStore = createRedisHistoryStore({
|
|
24
25
|
url: process.env.REDIS_URL as string,
|
|
25
26
|
ttlSeconds: 3600,
|
|
@@ -158,6 +159,20 @@ const messages = await client.getConversationMessages("conv_123");
|
|
|
158
159
|
console.log(messages);
|
|
159
160
|
```
|
|
160
161
|
|
|
162
|
+
## Redis
|
|
163
|
+
|
|
164
|
+
Use a single shared Redis client for history. Creating a new client per request can exhaust the Redis
|
|
165
|
+
`maxclients` limit and cause errors. The SDK provides `createRedisHistoryStore` to reuse a shared
|
|
166
|
+
client per URL, or you can pass your own client if you already manage one.
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
import Redis from "ioredis";
|
|
170
|
+
import { createRedisHistoryStore } from "@telygent/ai-sdk";
|
|
171
|
+
|
|
172
|
+
const redisClient = new Redis(process.env.REDIS_URL as string);
|
|
173
|
+
const historyStore = createRedisHistoryStore({ client: redisClient });
|
|
174
|
+
```
|
|
175
|
+
|
|
161
176
|
## Notes
|
|
162
177
|
|
|
163
178
|
- Conversation history lives in your Redis and is sent to Telygent each request.
|
|
@@ -166,22 +181,76 @@ console.log(messages);
|
|
|
166
181
|
- Use `requiredFilters` to inject fixed constraints into queries.
|
|
167
182
|
- Custom services are optional and let the AI call your own per-model lookup handlers.
|
|
168
183
|
- Use `getConversationMessages` to reload message history from Redis.
|
|
169
|
-
|
|
184
|
+
|
|
185
|
+
## Streaming (SSE)
|
|
186
|
+
|
|
187
|
+
Use in an Express route to stream events to the browser:
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
app.post("/stream", async (req, res) => {
|
|
191
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
192
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
193
|
+
res.setHeader("Connection", "keep-alive");
|
|
194
|
+
|
|
195
|
+
await client.queryStream(req.body, (event) => {
|
|
196
|
+
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
197
|
+
if (event.phase === "final" || event.phase === "error") {
|
|
198
|
+
res.end();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Use streaming when you want step-by-step updates (planning, tool execution, response). The stream emits
|
|
205
|
+
JSON events with a `phase` field:
|
|
206
|
+
|
|
207
|
+
- `thinking`: progress updates
|
|
208
|
+
- `final`: final response payload
|
|
209
|
+
- `error`: terminal error
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
const response = await client.queryStream(
|
|
213
|
+
{
|
|
214
|
+
question: "Show me recent transactions",
|
|
215
|
+
conversationId: "conv_123",
|
|
216
|
+
userContext: { userId: "user_1" },
|
|
217
|
+
},
|
|
218
|
+
(event) => {
|
|
219
|
+
if (event.phase === "thinking") {
|
|
220
|
+
console.log("step:", event.content);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (event.phase === "final") {
|
|
224
|
+
console.log("final:", event.content);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
console.log("answer:", response.content);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Notes:
|
|
233
|
+
- Streaming does not replace the existing `query()` method. Use either one.
|
|
234
|
+
- Only `phase: "final"` messages are stored in Redis history.
|
|
170
235
|
|
|
171
236
|
## Express middleware
|
|
172
237
|
|
|
173
238
|
```ts
|
|
174
239
|
import express from "express";
|
|
175
|
-
import { attachAiClient,
|
|
240
|
+
import { attachAiClient, createRedisHistoryStore, createMongoAdapter } from "@telygent/ai-sdk";
|
|
176
241
|
|
|
177
242
|
const app = express();
|
|
178
243
|
app.use(express.json());
|
|
179
244
|
|
|
245
|
+
const historyStore = createRedisHistoryStore({
|
|
246
|
+
url: process.env.REDIS_URL as string,
|
|
247
|
+
});
|
|
248
|
+
|
|
180
249
|
app.use(
|
|
181
250
|
attachAiClient({
|
|
182
251
|
apiKey: process.env.TELYGENT_API_KEY as string,
|
|
183
252
|
registry,
|
|
184
|
-
historyStore
|
|
253
|
+
historyStore,
|
|
185
254
|
dbAdapter: createMongoAdapter({ db: mongoDb, registry }),
|
|
186
255
|
})
|
|
187
256
|
);
|
package/dist/adapters/mongo.js
CHANGED
|
@@ -1 +1,205 @@
|
|
|
1
|
-
const _0x919f66=_0x5bec;(function(_0x3157ca,_0x3eae4c){const _0x49b0dc=_0x5bec,_0x4910da=_0x3157ca();while(!![]){try{const _0x28b7cf=parseInt(_0x49b0dc(0x140))/0x1+-parseInt(_0x49b0dc(0x160))/0x2+-parseInt(_0x49b0dc(0x17d))/0x3+parseInt(_0x49b0dc(0x136))/0x4*(parseInt(_0x49b0dc(0x188))/0x5)+-parseInt(_0x49b0dc(0x190))/0x6*(parseInt(_0x49b0dc(0x197))/0x7)+-parseInt(_0x49b0dc(0x161))/0x8+parseInt(_0x49b0dc(0x135))/0x9*(parseInt(_0x49b0dc(0x132))/0xa);if(_0x28b7cf===_0x3eae4c)break;else _0x4910da['push'](_0x4910da['shift']());}catch(_0xefe55){_0x4910da['push'](_0x4910da['shift']());}}}(_0x1ec4,0x7f7f1));const _0x21fefd=(function(){let _0x58daf9=!![];return function(_0x13d7d6,_0x36316c){const _0x4d7744=_0x58daf9?function(){if(_0x36316c){const _0x36b474=_0x36316c['apply'](_0x13d7d6,arguments);return _0x36316c=null,_0x36b474;}}:function(){};return _0x58daf9=![],_0x4d7744;};}()),_0x1635cc=_0x21fefd(this,function(){const _0x5ca39e=_0x5bec,_0x51c059={'gfZGY':_0x5ca39e(0x18c)};return _0x1635cc[_0x5ca39e(0x180)]()['search'](_0x5ca39e(0x18c))['toString']()['constructor'](_0x1635cc)[_0x5ca39e(0x142)](_0x51c059['gfZGY']);});function _0x5bec(_0x412c11,_0x237757){_0x412c11=_0x412c11-0x117;const _0x354a4e=_0x1ec4();let _0xf26d45=_0x354a4e[_0x412c11];if(_0x5bec['dhOsNn']===undefined){var _0x3cde4c=function(_0x21fefd){const _0x1ec480='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';let _0x5bec92='',_0x4a85f6='',_0x3b0f16=_0x5bec92+_0x3cde4c;for(let _0x5a2b23=0x0,_0x140832,_0x5b55e2,_0xe1ffcb=0x0;_0x5b55e2=_0x21fefd['charAt'](_0xe1ffcb++);~_0x5b55e2&&(_0x140832=_0x5a2b23%0x4?_0x140832*0x40+_0x5b55e2:_0x5b55e2,_0x5a2b23++%0x4)?_0x5bec92+=_0x3b0f16['charCodeAt'](_0xe1ffcb+0xa)-0xa!==0x0?String['fromCharCode'](0xff&_0x140832>>(-0x2*_0x5a2b23&0x6)):_0x5a2b23:0x0){_0x5b55e2=_0x1ec480['indexOf'](_0x5b55e2);}for(let _0x2e389b=0x0,_0x4ab8f4=_0x5bec92['length'];_0x2e389b<_0x4ab8f4;_0x2e389b++){_0x4a85f6+='%'+('00'+_0x5bec92['charCodeAt'](_0x2e389b)['toString'](0x10))['slice'](-0x2);}return decodeURIComponent(_0x4a85f6);};_0x5bec['iBcEtm']=_0x3cde4c,_0x5bec['aRnzfj']={},_0x5bec['dhOsNn']=!![];}const _0x568ede=_0x354a4e[0x0],_0x34a9b6=_0x412c11+_0x568ede,_0x1635cc=_0x5bec['aRnzfj'][_0x34a9b6];if(!_0x1635cc){const _0x421906=function(_0x7b319b){this['UKmgvT']=_0x7b319b,this['BiaSHt']=[0x1,0x0,0x0],this['WjAqXN']=function(){return'newState';},this['RvfLBa']='\x5cw+\x20*\x5c(\x5c)\x20*{\x5cw+\x20*',this['ZxNcJU']='[\x27|\x22].+[\x27|\x22];?\x20*}';};_0x421906['prototype']['kDKdds']=function(){const _0x3ae3a7=new RegExp(this['RvfLBa']+this['ZxNcJU']),_0x1f9773=_0x3ae3a7['test'](this['WjAqXN']['toString']())?--this['BiaSHt'][0x1]:--this['BiaSHt'][0x0];return this['EuFugZ'](_0x1f9773);},_0x421906['prototype']['EuFugZ']=function(_0x533b69){if(!Boolean(~_0x533b69))return _0x533b69;return this['wPvfcZ'](this['UKmgvT']);},_0x421906['prototype']['wPvfcZ']=function(_0x2718b5){for(let _0x4f8415=0x0,_0x58d944=this['BiaSHt']['length'];_0x4f8415<_0x58d944;_0x4f8415++){this['BiaSHt']['push'](Math['round'](Math['random']())),_0x58d944=this['BiaSHt']['length'];}return _0x2718b5(this['BiaSHt'][0x0]);},new _0x421906(_0x5bec)['kDKdds'](),_0xf26d45=_0x5bec['iBcEtm'](_0xf26d45),_0x5bec['aRnzfj'][_0x34a9b6]=_0xf26d45;}else _0xf26d45=_0x1635cc;return _0xf26d45;}_0x1635cc();const _0x3cde4c=(function(){const _0x29220d=_0x5bec,_0x56d31c={'UxUul':_0x29220d(0x1aa),'PcssP':function(_0x110e13,_0x3d7f42){return _0x110e13===_0x3d7f42;},'rVWTF':_0x29220d(0x19e),'DylcP':_0x29220d(0x182),'bSgIN':function(_0x2b1d9a,_0x32bbd4){return _0x2b1d9a===_0x32bbd4;},'NvfhU':_0x29220d(0x123),'CHrlP':_0x29220d(0x13b)};let _0x2819e6=!![];return function(_0x3eca8c,_0xc277f6){const _0x17efcd=_0x2819e6?function(){const _0x14cd49=_0x5bec,_0x4aa56c={'fUktW':_0x56d31c['UxUul'],'oLTIG':function(_0x34771b,_0x3c540c){return _0x34771b>_0x3c540c;},'JfcpX':function(_0x371c00,_0x509fc9){return _0x371c00(_0x509fc9);}};if(_0x56d31c[_0x14cd49(0x124)](_0x56d31c['rVWTF'],_0x56d31c[_0x14cd49(0x18d)])){const _0x3dfef6=_0x352428[_0x14cd49(0x148)],_0x33ac9d=_0x25ee40[_0x14cd49(0x1ac)]||_0x500af5['path'],_0x5d75a1=_0x25c051[_0x14cd49(0x145)]||_0x4aa56c[_0x14cd49(0x143)],_0x221e93={'from':_0x437ba1['target'],'localField':_0x33ac9d,'foreignField':_0x5d75a1,'as':_0x3dfef6};if(_0x3a3b61[_0x14cd49(0x11e)](_0x2b2937['select'])&&_0x4aa56c['oLTIG'](_0x710278[_0x14cd49(0x11a)][_0x14cd49(0x12c)],0x0)){const _0x415d06=_0x4aa56c[_0x14cd49(0x14b)](_0x3eb0ff,_0x1c94e0['select']);_0x221e93[_0x14cd49(0x192)]=_0x415d06?[{'$project':_0x415d06}]:void 0x0;}_0x16add1[_0x14cd49(0x12f)]({'$lookup':_0x221e93}),_0x2ede41[_0x14cd49(0x17b)]&&_0x56667f[_0x14cd49(0x12f)]({'$unwind':{'path':'$'+_0x3dfef6,'preserveNullAndEmptyArrays':!0x0}});}else{if(_0xc277f6){if(_0x56d31c[_0x14cd49(0x149)](_0x56d31c[_0x14cd49(0x139)],_0x56d31c[_0x14cd49(0x16a)])){const _0x254c27=_0xac9184[_0x14cd49(0x170)][_0x14cd49(0x178)][_0x14cd49(0x1a2)](_0x3c7398),_0x59af5d=_0x247bd9[_0xcdd392],_0x227af9=_0x33dcf5[_0x59af5d]||_0x254c27;_0x254c27[_0x14cd49(0x174)]=_0xd3607b['bind'](_0x404355),_0x254c27['toString']=_0x227af9[_0x14cd49(0x180)][_0x14cd49(0x1a2)](_0x227af9),_0x36fbf7[_0x59af5d]=_0x254c27;}else{const _0x39d737=_0xc277f6[_0x14cd49(0x198)](_0x3eca8c,arguments);return _0xc277f6=null,_0x39d737;}}}}:function(){};return _0x2819e6=![],_0x17efcd;};}()),_0xf26d45=_0x3cde4c(this,function(){const _0x561476=_0x5bec,_0x586bcd={'oQWlo':function(_0x2990c1,_0x52527b){return _0x2990c1+_0x52527b;},'UwBoR':function(_0x3e2867,_0x1e9263){return _0x3e2867+_0x1e9263;},'BeceR':function(_0x281a79,_0x3738c9){return _0x281a79!==_0x3738c9;},'UEdKP':_0x561476(0x122),'UYkJe':_0x561476(0x1a9),'unpbw':_0x561476(0x181),'jMPZy':_0x561476(0x1a0),'JCQIx':function(_0x2d6d87,_0x2191a5){return _0x2d6d87(_0x2191a5);},'pyxKN':_0x561476(0x14d),'KTolR':'{}.constructor(\x22return\x20this\x22)(\x20)','cNXSp':function(_0x4903c3,_0xd5432a){return _0x4903c3!==_0xd5432a;},'WfNZn':_0x561476(0x12b),'joDjv':_0x561476(0x184),'bSaYt':function(_0x258994,_0x27a984){return _0x258994(_0x27a984);},'ySdSf':function(_0x3db5ea){return _0x3db5ea();},'GfFhM':'log','ldfho':'warn','JDuOK':_0x561476(0x11d),'EcNOP':_0x561476(0x164),'cabyo':'exception','YqzMz':'trace','epwbb':function(_0x25c997,_0x5e6fd2){return _0x25c997<_0x5e6fd2;},'Qahwe':_0x561476(0x117)},_0x571d99=function(){const _0x1caffc=_0x561476,_0x4ea868={'liVFN':function(_0x9ab89d,_0x2f7dd9){const _0x440fcb=_0x5bec;return _0x586bcd[_0x440fcb(0x15a)](_0x9ab89d,_0x2f7dd9);},'OjGQK':function(_0x473476,_0x40584c){const _0x42cfa7=_0x5bec;return _0x586bcd[_0x42cfa7(0x13f)](_0x473476,_0x40584c);},'hAvEa':_0x1caffc(0x14d)};if(_0x586bcd[_0x1caffc(0x159)](_0x586bcd[_0x1caffc(0x133)],_0x586bcd[_0x1caffc(0x11f)])){let _0x415eb3;try{if(_0x586bcd[_0x1caffc(0x130)]===_0x586bcd[_0x1caffc(0x199)]){let _0x46ee05;try{_0x46ee05=_0x26e443(_0x4ea868[_0x1caffc(0x1a4)](_0x4ea868[_0x1caffc(0x134)](_0x4ea868[_0x1caffc(0x163)],'{}.constructor(\x22return\x20this\x22)(\x20)'),');'))();}catch(_0x13513a){_0x46ee05=_0x54c7da;}return _0x46ee05;}else _0x415eb3=_0x586bcd[_0x1caffc(0x118)](Function,_0x586bcd['UwBoR'](_0x586bcd['UwBoR'](_0x586bcd[_0x1caffc(0x151)],_0x586bcd[_0x1caffc(0x157)]),');'))();}catch(_0x5dff52){if(_0x586bcd[_0x1caffc(0x187)](_0x586bcd[_0x1caffc(0x154)],_0x586bcd[_0x1caffc(0x15b)]))_0x415eb3=window;else{if(_0x11dec6){const _0x1a6f63=_0x5e34e3['apply'](_0x1f8e6f,arguments);return _0x383353=null,_0x1a6f63;}}}return _0x415eb3;}else _0x4231ca=_0x2ef23f;},_0xc5b6b9=_0x586bcd[_0x561476(0x166)](_0x571d99),_0x265327=_0xc5b6b9[_0x561476(0x131)]=_0xc5b6b9[_0x561476(0x131)]||{},_0x237793=[_0x586bcd['GfFhM'],_0x586bcd[_0x561476(0x19d)],_0x586bcd[_0x561476(0x15c)],_0x586bcd[_0x561476(0x17e)],_0x586bcd[_0x561476(0x11c)],'table',_0x586bcd[_0x561476(0x16b)]];for(let _0x19f062=0x0;_0x586bcd['epwbb'](_0x19f062,_0x237793['length']);_0x19f062++){if(_0x586bcd['Qahwe']===_0x586bcd[_0x561476(0x194)]){const _0x898033=_0x3cde4c[_0x561476(0x170)][_0x561476(0x178)]['bind'](_0x3cde4c),_0xae42a9=_0x237793[_0x19f062],_0x457e95=_0x265327[_0xae42a9]||_0x898033;_0x898033[_0x561476(0x174)]=_0x3cde4c[_0x561476(0x1a2)](_0x3cde4c),_0x898033[_0x561476(0x180)]=_0x457e95[_0x561476(0x180)][_0x561476(0x1a2)](_0x457e95),_0x265327[_0xae42a9]=_0x898033;}else{const _0x108705=_0x586bcd['bSaYt'](_0x298fec,_0xf5e7fc[_0x561476(0x11a)]);_0x1cf9a9['pipeline']=_0x108705?[{'$project':_0x108705}]:void 0x0;}}});_0xf26d45();'use strict';function buildProjection(_0x464170){const _0x3a727a=_0x5bec,_0x297eb5={'bjksH':function(_0x587830,_0x3618d8){return _0x587830===_0x3618d8;},'rEopA':function(_0x25e62a,_0x100be9){return _0x25e62a===_0x100be9;},'PygsU':_0x3a727a(0x1a6),'ktDEv':_0x3a727a(0x12e)};if(!_0x464170||_0x297eb5[_0x3a727a(0x155)](0x0,_0x464170[_0x3a727a(0x12c)]))return;const _0x5daebc=[...new Set(_0x464170)][_0x3a727a(0x19f)](_0x13ea63=>_0x3a727a(0x183)==typeof _0x13ea63&&_0x13ea63[_0x3a727a(0x12c)]>0x0)[_0x3a727a(0x12a)]((_0x56c54e,_0x5437c7)=>_0x5437c7['length']-_0x56c54e['length']),_0x333970={};for(const _0x1e7ddf of _0x5daebc){if(_0x297eb5[_0x3a727a(0x176)](_0x297eb5['PygsU'],_0x297eb5[_0x3a727a(0x19c)])){const _0x238ff0=_0x533b69?function(){const _0x46ad08=_0x3a727a;if(_0x15b644){const _0x349ba6=_0x5e9c55[_0x46ad08(0x198)](_0x257988,arguments);return _0x48d2e9=null,_0x349ba6;}}:function(){};return _0x1c178d=![],_0x238ff0;}else{if(Object[_0x3a727a(0x165)](_0x333970)['some'](_0xac8fc1=>_0xac8fc1===_0x1e7ddf||_0xac8fc1[_0x3a727a(0x141)](_0x1e7ddf+'.')))continue;Object[_0x3a727a(0x165)](_0x333970)['some'](_0x4a1d76=>_0x1e7ddf['startsWith'](_0x4a1d76+'.'))||(_0x333970[_0x1e7ddf]=0x1);}}return Object['keys'](_0x333970)[_0x3a727a(0x12c)]>0x0?_0x333970:void 0x0;}function createMongoAdapter(_0x5c9066){const _0x48f114=_0x5bec,_0x559d8e={'brlRT':function(_0x3f9239,_0x129921){return _0x3f9239+_0x129921;},'RdLtG':_0x48f114(0x11b),'NRLgF':function(_0x19a8bd,_0x233943){return _0x19a8bd!==_0x233943;},'ibGsF':'Xbqmy','SPIaU':function(_0x441564,_0x49384c){return _0x441564===_0x49384c;},'FtALs':function(_0x1ce0dd,_0x26f8f6){return _0x1ce0dd(_0x26f8f6);},'QAsPg':function(_0x1c92b6,_0x340a52){return _0x1c92b6>_0x340a52;},'HwMyc':_0x48f114(0x120),'iTPex':function(_0x3d6159,_0x1e2d43){return _0x3d6159>_0x1e2d43;},'fNUYW':function(_0x57d29f,_0x4d2fcd){return _0x57d29f===_0x4d2fcd;},'NBXaq':'yRTlh','eeZFo':'IuXov','aEZLN':function(_0x54da94,_0x2fb244){return _0x54da94 in _0x2fb244;},'aXcZJ':_0x48f114(0x173),'ssMWw':_0x48f114(0x1a3),'BkTSh':'sort','enpAH':_0x48f114(0x15f),'NFynr':function(_0x2d4a2c,_0x4f100b){return _0x2d4a2c==_0x4f100b;},'bJPIf':'string','eCwfn':_0x48f114(0x18c),'KPsaj':_0x48f114(0x127),'IFQLv':function(_0x4bad3d,_0x19ac49){return _0x4bad3d-_0x19ac49;},'msNob':function(_0x4ed219,_0x382ff6){return _0x4ed219>_0x382ff6;},'sfIWp':function(_0x242ad4,_0x57d0b2){return _0x242ad4(_0x57d0b2);}},{db:_0xab62bf,registry:_0x514e17,defaultLimit:_0x566e61=0xa}=_0x5c9066;return{async 'queryDocuments'(_0x242517){const _0x3749e8=_0x48f114;if(_0x559d8e[_0x3749e8(0x189)](_0x559d8e[_0x3749e8(0x13d)],_0x559d8e[_0x3749e8(0x13d)]))_0x33c8e3=_0x270e85(AUUZwJ[_0x3749e8(0x16e)]('return\x20(function()\x20',AUUZwJ[_0x3749e8(0x150)])+');')();else{const _0x4a51d9=String(_0x242517[_0x3749e8(0x14c)]||''),_0x152076=_0x514e17[_0x4a51d9];if(!_0x152076)throw new Error(_0x3749e8(0x177)+_0x4a51d9+_0x3749e8(0x13a));const _0x2aabe9=_0xab62bf[_0x3749e8(0x158)](_0x152076['collectionName']),_0x2935dc=_0x242517['filters']||{},_0x473b59=Number(_0x242517[_0x3749e8(0x125)]??_0x566e61),_0x23f5ff=Number(_0x242517['skip']??0x0),_0x58ac47=_0x242517[_0x3749e8(0x1a5)]||{'createdAt':-0x1},_0x2bf85f=Array[_0x3749e8(0x11e)](_0x242517[_0x3749e8(0x14e)])?_0x242517['fields']:_0x152076[_0x3749e8(0x138)],_0x4c14d0=Array[_0x3749e8(0x11e)](_0x152076[_0x3749e8(0x18b)])?_0x152076[_0x3749e8(0x18b)]:[],_0x3e1db8=Number[_0x3749e8(0x144)](_0x473b59)?Math[_0x3749e8(0x152)](0x0,_0x473b59):_0x566e61,_0x3e9a0d=_0x3e1db8>0x0?_0x559d8e[_0x3749e8(0x16e)](_0x3e1db8,0x1):0x0;if(0x0===_0x3e9a0d)return{'model':_0x4a51d9,'documents':[],'limit':_0x3e1db8,'skip':_0x23f5ff,'results_count':0x0,'has_more':!0x1};if(_0x559d8e[_0x3749e8(0x146)](0x0,_0x4c14d0[_0x3749e8(0x12c)])){const _0x15e08c=_0x559d8e['FtALs'](buildProjection,_0x2bf85f),_0x32ce95=_0x2aabe9[_0x3749e8(0x18a)](_0x2935dc,_0x15e08c?{'projection':_0x15e08c}:void 0x0)[_0x3749e8(0x12a)](_0x58ac47)[_0x3749e8(0x175)](_0x23f5ff)['limit'](_0x3e9a0d),_0x4f7163=await _0x32ce95[_0x3749e8(0x1a7)](),_0x3b0d27=_0x559d8e['QAsPg'](_0x4f7163[_0x3749e8(0x12c)],_0x3e1db8),_0x24d570=_0x3b0d27?_0x4f7163['slice'](0x0,_0x3e1db8):_0x4f7163;return{'model':_0x4a51d9,'documents':_0x24d570,'limit':_0x3e1db8,'skip':_0x23f5ff,'results_count':_0x24d570[_0x3749e8(0x12c)],'has_more':_0x3b0d27};}const _0x165061=[..._0x2bf85f];for(const _0x5061a6 of _0x4c14d0)_0x5061a6?.[_0x3749e8(0x148)]&&!_0x165061[_0x3749e8(0x193)](_0x5061a6[_0x3749e8(0x148)])&&_0x165061['push'](_0x5061a6[_0x3749e8(0x148)]);const _0x304825=_0x559d8e[_0x3749e8(0x119)](buildProjection,_0x165061),_0x4fb193=[{'$match':_0x2935dc},{'$sort':_0x58ac47},{'$skip':_0x23f5ff},{'$limit':_0x3e9a0d}];for(const _0x382993 of _0x4c14d0){const _0x1f5486=_0x382993[_0x3749e8(0x148)],_0x5db615=_0x382993['localField']||_0x382993[_0x3749e8(0x148)],_0x37aa07=_0x382993['foreignField']||_0x3749e8(0x1aa),_0x1c8295={'from':_0x382993[_0x3749e8(0x153)],'localField':_0x5db615,'foreignField':_0x37aa07,'as':_0x1f5486};if(Array['isArray'](_0x382993[_0x3749e8(0x11a)])&&_0x382993[_0x3749e8(0x11a)][_0x3749e8(0x12c)]>0x0){if(_0x559d8e[_0x3749e8(0x146)](_0x559d8e[_0x3749e8(0x1a1)],_0x559d8e[_0x3749e8(0x1a1)])){const _0x210413=_0x559d8e['FtALs'](buildProjection,_0x382993['select']);_0x1c8295[_0x3749e8(0x192)]=_0x210413?[{'$project':_0x210413}]:void 0x0;}else{const _0x57f1bf=_0x5c69dd[_0x3749e8(0x198)](_0x54a640,arguments);return _0x4b01fe=null,_0x57f1bf;}}_0x4fb193[_0x3749e8(0x12f)]({'$lookup':_0x1c8295}),_0x382993['singleResult']&&_0x4fb193[_0x3749e8(0x12f)]({'$unwind':{'path':'$'+_0x1f5486,'preserveNullAndEmptyArrays':!0x0}});}_0x304825&&_0x4fb193[_0x3749e8(0x12f)]({'$project':_0x304825});const _0x155f7d=await _0x2aabe9[_0x3749e8(0x14a)](_0x4fb193)[_0x3749e8(0x1a7)](),_0x5e75b5=_0x559d8e[_0x3749e8(0x1a8)](_0x155f7d[_0x3749e8(0x12c)],_0x3e1db8),_0x1c2309=_0x5e75b5?_0x155f7d[_0x3749e8(0x19b)](0x0,_0x3e1db8):_0x155f7d;return{'model':_0x4a51d9,'documents':_0x1c2309,'limit':_0x3e1db8,'skip':_0x23f5ff,'results_count':_0x1c2309[_0x3749e8(0x12c)],'has_more':_0x5e75b5};}},async 'aggregateDocuments'(_0x4f8d38){const _0x347966=_0x48f114,_0x3999ab={'ihPFL':function(_0x5dea65,_0x33ee93){const _0x446d4d=_0x5bec;return _0x559d8e[_0x446d4d(0x172)](_0x5dea65,_0x33ee93);},'bOkSh':_0x559d8e[_0x347966(0x1ae)],'qNubA':_0x559d8e['eeZFo'],'wTffq':function(_0x51d2b5,_0x2ea248){return _0x51d2b5!=_0x2ea248;},'jFPtw':_0x347966(0x171),'RfWKR':function(_0x292a45,_0x1f454a){const _0x58d652=_0x347966;return _0x559d8e[_0x58d652(0x18e)](_0x292a45,_0x1f454a);},'JKCUa':_0x347966(0x16c),'qKyej':_0x559d8e[_0x347966(0x128)],'xjxxu':_0x559d8e[_0x347966(0x162)],'nECwc':_0x559d8e[_0x347966(0x17f)],'UkFjN':_0x347966(0x147),'zMwsK':_0x347966(0x15d),'eJAYU':_0x559d8e[_0x347966(0x1ab)],'MfKRv':function(_0x101bdc,_0x260f8c){const _0xa598fc=_0x347966;return _0x559d8e[_0xa598fc(0x18f)](_0x101bdc,_0x260f8c);},'VjYpC':_0x559d8e['bJPIf'],'Oapbd':_0x347966(0x168),'wPUoO':function(_0x7188a1,_0x30bf66){const _0x5c8d82=_0x347966;return _0x559d8e[_0x5c8d82(0x18f)](_0x7188a1,_0x30bf66);},'QfJwe':_0x559d8e['eCwfn']};if(_0x347966(0x127)!==_0x559d8e[_0x347966(0x15e)]){const _0x30aa26=_0xb645ff[0x0],_0x164fba=_0x3c9136[_0x347966(0x165)](_0x30aa26)[0x0],_0x36c50c=_0x164fba?_0x559d8e[_0x347966(0x119)](_0x1ceca0,_0x30aa26[_0x164fba]):_0x191ebe;_0x34a07['isNaN'](_0x36c50c)||(_0x3ddebd=_0x36c50c);}else{const _0x598d69=_0x559d8e[_0x347966(0x119)](String,_0x4f8d38[_0x347966(0x14c)]||''),_0x365741=_0x514e17[_0x598d69];if(!_0x365741)throw new Error('Model\x20\x27'+_0x598d69+'\x27\x20is\x20not\x20registered');const _0x3946f6=_0xab62bf[_0x347966(0x158)](_0x365741[_0x347966(0x137)]),_0x35e053=Array[_0x347966(0x11e)](_0x4f8d38[_0x347966(0x192)])?_0x4f8d38[_0x347966(0x192)]:[],_0x1e1d49=function(_0x13e35b){const _0x37c0b2=_0x347966,_0x155fc4={'TaMuA':_0x3999ab[_0x37c0b2(0x1ad)]};return _0x13e35b[_0x37c0b2(0x179)](_0x562beb=>{const _0x1381b3=_0x37c0b2;if(_0x3999ab[_0x1381b3(0x17c)](_0x3999ab['bOkSh'],_0x3999ab[_0x1381b3(0x186)]))return _0x3f1b5f['toString']()[_0x1381b3(0x142)](SmBEuh[_0x1381b3(0x12d)])['toString']()['constructor'](_0xa7913)['search'](SmBEuh[_0x1381b3(0x12d)]);else{if(!_0x562beb||_0x3999ab['wTffq'](_0x3999ab[_0x1381b3(0x185)],typeof _0x562beb))return _0x562beb;if(!_0x3999ab[_0x1381b3(0x16d)](_0x3999ab['JKCUa'],_0x562beb))return _0x562beb;const _0x21ed10=_0x562beb[_0x1381b3(0x16c)],_0x5de570=_0x562beb[_0x1381b3(0x169)];switch(_0x21ed10){case _0x3999ab[_0x1381b3(0x14f)]:return{'$match':_0x5de570};case _0x3999ab[_0x1381b3(0x129)]:return{'$group':_0x5de570};case _0x3999ab['nECwc']:return{'$sort':_0x5de570};case _0x1381b3(0x125):return{'$limit':_0x3999ab[_0x1381b3(0x196)]==typeof _0x5de570?_0x5de570:_0x5de570?.[_0x1381b3(0x125)]??0x64};case'project':return{'$project':_0x5de570};case _0x3999ab[_0x1381b3(0x13c)]:return{'$addFields':_0x5de570};case _0x3999ab[_0x1381b3(0x17a)]:return{'$count':_0x3999ab['MfKRv'](_0x3999ab[_0x1381b3(0x191)],typeof _0x5de570)?_0x5de570:_0x5de570?.['field']??_0x3999ab[_0x1381b3(0x17a)]};case _0x3999ab['Oapbd']:return{'$unwind':_0x3999ab[_0x1381b3(0x156)](_0x3999ab['VjYpC'],typeof _0x5de570)?_0x5de570:_0x5de570?.[_0x1381b3(0x148)]};default:return _0x562beb;}}});}(_0x35e053),_0x1ceefd=await _0x3946f6[_0x347966(0x14a)](_0x1e1d49)['toArray']();let _0x40ea3b;const _0x3118e9=_0x35e053[_0x559d8e[_0x347966(0x121)](_0x35e053[_0x347966(0x12c)],0x1)];if(_0x559d8e[_0x347966(0x1ab)]===_0x3118e9?.[_0x347966(0x16c)]&&_0x559d8e['msNob'](_0x1ceefd[_0x347966(0x12c)],0x0)){const _0x921cdc=_0x1ceefd[0x0],_0x76dc4f=Object['keys'](_0x921cdc)[0x0],_0x4e7a60=_0x76dc4f?_0x559d8e[_0x347966(0x119)](Number,_0x921cdc[_0x76dc4f]):NaN;Number['isNaN'](_0x4e7a60)||(_0x40ea3b=_0x4e7a60);}return{'model':_0x598d69,'results':_0x1ceefd,'results_count':_0x1ceefd[_0x347966(0x12c)],'has_more':!0x1,..._0x559d8e[_0x347966(0x189)](void 0x0,_0x40ea3b)?{'total_count':_0x40ea3b}:{}};}},async 'getDocumentById'(_0x10ee84){const _0x26e2ff=_0x48f114,_0x254c34=String(_0x10ee84['model']||''),_0x69e33c=_0x514e17[_0x254c34];if(!_0x69e33c)throw new Error(_0x26e2ff(0x177)+_0x254c34+_0x26e2ff(0x13a));const _0x2812a9=_0xab62bf['collection'](_0x69e33c[_0x26e2ff(0x137)]),_0x5b37df=_0x10ee84[_0x26e2ff(0x195)],_0x34e362=_0x10ee84[_0x26e2ff(0x19a)]||{},_0x40995a=_0x559d8e[_0x26e2ff(0x167)](buildProjection,Array['isArray'](_0x10ee84[_0x26e2ff(0x14e)])?_0x10ee84['fields']:_0x69e33c[_0x26e2ff(0x138)]),_0x34d3c3={'_id':_0x5b37df,..._0x34e362},_0x5fdb81=await _0x2812a9[_0x26e2ff(0x13e)](_0x34d3c3,_0x40995a?{'projection':_0x40995a}:void 0x0);return _0x5fdb81?{'model':_0x254c34,'found':!0x0,'document':_0x5fdb81}:{'model':_0x254c34,'found':!0x1};}};}function _0x1ec4(){const _0x36fe7a=['AxnbCNjHEq','vvLRsMu','AvjQu1C','suzrthy','BMrrz00','wLP1shu','ugnZC1a','BgLTAxq','x19LC01VzhvSzq','ve1Rqxq','yvHJwKO','EgP4Ehu','C29YDa','yxnQvvu','BgvUz3rO','vgfnDue','D1j6AMK','ChvZAa','Dw5WyNC','y29UC29Szq','mtiWmgTlB1vnta','vuvKs1a','t2PhuuS','mtmXnZyWtMnosMjM','nhDRwgrjvW','y29SBgvJDgLVBK5HBwu','ywXSB3DLzezPzwXKCW','tNzMAfu','jYbPCYbUB3qGCMvNAxn0zxjLza','C2vXCw0','EK13C0S','AwjhC0y','zMLUze9Uzq','vxDcB1i','nJeZmdG3whvPz05R','C3rHCNrZv2L0Aa','C2vHCMnO','zLvRDfC','AxngAw5PDgu','zM9YzwLNBKzPzwXK','u1bjyvu','BNvTyMvY','Cgf0Aa','yLnNsu4','ywDNCMvNyxrL','sMzJCfG','Bw9KzwW','CMv0DxjUicHMDw5JDgLVBIGPia','zMLLBgrZ','CuT5zwO','uMrmDeC','ChL4s04','Bwf4','DgfYz2v0','v2zowM4','yMPRC0G','D1bvB08','s1rVBfi','y29SBgvJDgLVBG','qMvJzvi','B1fxBg8','AM9eANy','sKr1t0S','ywrKrMLLBgrZ','s1bZywO','y291BNq','mtCZnZC2mM94C0HlDa','nJaYodiYngvYAu5hzq','C3nnv3C','Aef2rwe','zxjYB3i','A2v5CW','EvnKu2y','C2zjv3a','Dw53Aw5K','CgfYyw1Z','q0HYBfa','wxf6txO','C3rHz2u','uMzxs1i','yNjSuLq','zgvMAw5LuhjVCgvYDhK','y29UC3rYDwn0B3i','B2jQzwn0','zK5vwvC','Bwf0y2G','x19WCM90B19F','C2TPCa','CKvVCee','tw9KzwWGjW','ChjVDg90ExbL','BwfW','zuPbwvu','C2LUz2XLuMvZDwX0','AwHqrKW','mZi0otKZvwrZr2Tc','rwnot1a','qMTuu2G','Dg9tDhjPBMC','yuHNuxy','EgrJtMK','C3rYAw5N','qvDltMK','AKzqDhC','Cu51yKe','y05yu3a','mtyYnZa1ALvqzuH1','tLjmz0y','zMLUza','CMvSyxrPB25Z','kcGOlISPkYKRksSK','rhLSy1a','yuvAte4','tKz5BNi','mtH1z1DwywS','vMPzCem','CgLWzwXPBMu','Aw5JBhvKzxm','uwfOD2u','zg9JDw1LBNrjza','vwTgAK4','mZq4nZq3EfHLsNHy','yxbWBhK','AK1qwNK','zMLSDgvYCW','C2XPy2u','A3rerxy','BgrMAg8','EfHNreW','zMLSDgvY','rLbyywq','shDnEwm','yMLUza','z3jVDxa','BgLwrK4','C29YDej5','q2Hfwfu','Dg9bCNjHEq','AvrqzxG','vvHprg8','x2LK','zw5WquG','Bg9JywXgAwvSza','uwzkD2u','tKjyyxe','EKHuz1O','sKnrsxG','rNrbthm','C2vSzwn0','E30Uy29UC3rYDwn0B3iOiNjLDhvYBIb0AgLZiIKOicK','y2fIEw8','Aw5MBW'];_0x1ec4=function(){return _0x36fe7a;};return _0x1ec4();}Object[_0x919f66(0x16f)](exports,_0x919f66(0x126),{'value':!0x0}),exports['createMongoAdapter']=createMongoAdapter;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMongoAdapter = createMongoAdapter;
|
|
4
|
+
function buildProjection(fields) {
|
|
5
|
+
if (!fields || fields.length === 0) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
const normalized = [...new Set(fields)]
|
|
9
|
+
.filter((field) => typeof field === "string" && field.length > 0)
|
|
10
|
+
.sort((a, b) => b.length - a.length);
|
|
11
|
+
const projection = {};
|
|
12
|
+
for (const field of normalized) {
|
|
13
|
+
const isParent = Object.keys(projection).some((existing) => existing === field || existing.startsWith(`${field}.`));
|
|
14
|
+
if (isParent) {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const isChild = Object.keys(projection).some((existing) => field.startsWith(`${existing}.`));
|
|
18
|
+
if (isChild) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
projection[field] = 1;
|
|
22
|
+
}
|
|
23
|
+
return Object.keys(projection).length > 0 ? projection : undefined;
|
|
24
|
+
}
|
|
25
|
+
function createMongoAdapter(options) {
|
|
26
|
+
const { db, registry, defaultLimit = 10 } = options;
|
|
27
|
+
function buildPipeline(pipeline) {
|
|
28
|
+
return pipeline.map((stage) => {
|
|
29
|
+
if (!stage || typeof stage !== "object") {
|
|
30
|
+
return stage;
|
|
31
|
+
}
|
|
32
|
+
if (!("stage" in stage)) {
|
|
33
|
+
return stage;
|
|
34
|
+
}
|
|
35
|
+
const stageName = stage.stage;
|
|
36
|
+
const params = stage.params;
|
|
37
|
+
switch (stageName) {
|
|
38
|
+
case "match":
|
|
39
|
+
return { $match: params };
|
|
40
|
+
case "group":
|
|
41
|
+
return { $group: params };
|
|
42
|
+
case "sort":
|
|
43
|
+
return { $sort: params };
|
|
44
|
+
case "limit":
|
|
45
|
+
return { $limit: typeof params === "number" ? params : params?.limit ?? 100 };
|
|
46
|
+
case "project":
|
|
47
|
+
return { $project: params };
|
|
48
|
+
case "addFields":
|
|
49
|
+
return { $addFields: params };
|
|
50
|
+
case "count":
|
|
51
|
+
return { $count: typeof params === "string" ? params : params?.field ?? "count" };
|
|
52
|
+
case "unwind":
|
|
53
|
+
return { $unwind: typeof params === "string" ? params : params?.path };
|
|
54
|
+
default:
|
|
55
|
+
return stage;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
async queryDocuments(params) {
|
|
61
|
+
const modelName = String(params.model || "");
|
|
62
|
+
const model = registry[modelName];
|
|
63
|
+
if (!model) {
|
|
64
|
+
throw new Error(`Model '${modelName}' is not registered`);
|
|
65
|
+
}
|
|
66
|
+
const collection = db.collection(model.collectionName);
|
|
67
|
+
const filters = params.filters || {};
|
|
68
|
+
const limit = Number(params.limit ?? defaultLimit);
|
|
69
|
+
const skip = Number(params.skip ?? 0);
|
|
70
|
+
const sortBy = params.sortBy || { createdAt: -1 };
|
|
71
|
+
const fields = Array.isArray(params.fields) ? params.fields : model.allowedFields;
|
|
72
|
+
const relations = Array.isArray(model.relations) ? model.relations : [];
|
|
73
|
+
const safeLimit = Number.isFinite(limit) ? Math.max(0, limit) : defaultLimit;
|
|
74
|
+
const fetchLimit = safeLimit > 0 ? safeLimit + 1 : 0;
|
|
75
|
+
if (fetchLimit === 0) {
|
|
76
|
+
return {
|
|
77
|
+
model: modelName,
|
|
78
|
+
documents: [],
|
|
79
|
+
limit: safeLimit,
|
|
80
|
+
skip,
|
|
81
|
+
results_count: 0,
|
|
82
|
+
has_more: false,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (relations.length === 0) {
|
|
86
|
+
const projection = buildProjection(fields);
|
|
87
|
+
const cursor = collection
|
|
88
|
+
.find(filters, projection ? { projection } : undefined)
|
|
89
|
+
.sort(sortBy)
|
|
90
|
+
.skip(skip)
|
|
91
|
+
.limit(fetchLimit);
|
|
92
|
+
const fetched = await cursor.toArray();
|
|
93
|
+
const has_more = fetched.length > safeLimit;
|
|
94
|
+
const documents = has_more ? fetched.slice(0, safeLimit) : fetched;
|
|
95
|
+
return {
|
|
96
|
+
model: modelName,
|
|
97
|
+
documents,
|
|
98
|
+
limit: safeLimit,
|
|
99
|
+
skip,
|
|
100
|
+
results_count: documents.length,
|
|
101
|
+
has_more,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const projectionFields = [...fields];
|
|
105
|
+
for (const relation of relations) {
|
|
106
|
+
if (relation?.path && !projectionFields.includes(relation.path)) {
|
|
107
|
+
projectionFields.push(relation.path);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const projection = buildProjection(projectionFields);
|
|
111
|
+
const pipeline = [
|
|
112
|
+
{ $match: filters },
|
|
113
|
+
{ $sort: sortBy },
|
|
114
|
+
{ $skip: skip },
|
|
115
|
+
{ $limit: fetchLimit },
|
|
116
|
+
];
|
|
117
|
+
for (const relation of relations) {
|
|
118
|
+
const path = relation.path;
|
|
119
|
+
const localField = relation.localField || relation.path;
|
|
120
|
+
const foreignField = relation.foreignField || "_id";
|
|
121
|
+
const lookup = {
|
|
122
|
+
from: relation.target,
|
|
123
|
+
localField,
|
|
124
|
+
foreignField,
|
|
125
|
+
as: path,
|
|
126
|
+
};
|
|
127
|
+
if (Array.isArray(relation.select) && relation.select.length > 0) {
|
|
128
|
+
const selectProjection = buildProjection(relation.select);
|
|
129
|
+
lookup.pipeline = selectProjection
|
|
130
|
+
? [{ $project: selectProjection }]
|
|
131
|
+
: undefined;
|
|
132
|
+
}
|
|
133
|
+
pipeline.push({ $lookup: lookup });
|
|
134
|
+
if (relation.singleResult) {
|
|
135
|
+
pipeline.push({
|
|
136
|
+
$unwind: {
|
|
137
|
+
path: `$${path}`,
|
|
138
|
+
preserveNullAndEmptyArrays: true,
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (projection) {
|
|
144
|
+
pipeline.push({ $project: projection });
|
|
145
|
+
}
|
|
146
|
+
const fetched = await collection.aggregate(pipeline).toArray();
|
|
147
|
+
const has_more = fetched.length > safeLimit;
|
|
148
|
+
const documents = has_more ? fetched.slice(0, safeLimit) : fetched;
|
|
149
|
+
return {
|
|
150
|
+
model: modelName,
|
|
151
|
+
documents,
|
|
152
|
+
limit: safeLimit,
|
|
153
|
+
skip,
|
|
154
|
+
results_count: documents.length,
|
|
155
|
+
has_more,
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
async aggregateDocuments(params) {
|
|
159
|
+
const modelName = String(params.model || "");
|
|
160
|
+
const model = registry[modelName];
|
|
161
|
+
if (!model) {
|
|
162
|
+
throw new Error(`Model '${modelName}' is not registered`);
|
|
163
|
+
}
|
|
164
|
+
const collection = db.collection(model.collectionName);
|
|
165
|
+
const pipeline = Array.isArray(params.pipeline) ? params.pipeline : [];
|
|
166
|
+
const mongoPipeline = buildPipeline(pipeline);
|
|
167
|
+
const results = await collection.aggregate(mongoPipeline).toArray();
|
|
168
|
+
let total_count;
|
|
169
|
+
const lastStage = pipeline[pipeline.length - 1];
|
|
170
|
+
if (lastStage?.stage === "count" && results.length > 0) {
|
|
171
|
+
const first = results[0];
|
|
172
|
+
const countKey = Object.keys(first)[0];
|
|
173
|
+
const countValue = countKey ? Number(first[countKey]) : NaN;
|
|
174
|
+
if (!Number.isNaN(countValue)) {
|
|
175
|
+
total_count = countValue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
model: modelName,
|
|
180
|
+
results,
|
|
181
|
+
results_count: results.length,
|
|
182
|
+
has_more: false,
|
|
183
|
+
...(total_count !== undefined ? { total_count } : {}),
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
async getDocumentById(params) {
|
|
187
|
+
const modelName = String(params.model || "");
|
|
188
|
+
const model = registry[modelName];
|
|
189
|
+
if (!model) {
|
|
190
|
+
throw new Error(`Model '${modelName}' is not registered`);
|
|
191
|
+
}
|
|
192
|
+
const collection = db.collection(model.collectionName);
|
|
193
|
+
const documentId = params.documentId;
|
|
194
|
+
const filters = params.filters || {};
|
|
195
|
+
const fields = Array.isArray(params.fields) ? params.fields : model.allowedFields;
|
|
196
|
+
const projection = buildProjection(fields);
|
|
197
|
+
const query = { _id: documentId, ...filters };
|
|
198
|
+
const document = await collection.findOne(query, projection ? { projection } : undefined);
|
|
199
|
+
if (!document) {
|
|
200
|
+
return { model: modelName, found: false };
|
|
201
|
+
}
|
|
202
|
+
return { model: modelName, found: true, document };
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ClientConfig, ConversationMessage, QueryRequest, QueryResponse } from "./types";
|
|
1
|
+
import { ClientConfig, ConversationMessage, QueryRequest, QueryResponse, StreamEvent } from "./types";
|
|
2
2
|
declare class AiClient {
|
|
3
3
|
private apiKey;
|
|
4
4
|
private registryVersion;
|
|
@@ -18,6 +18,7 @@ declare class AiClient {
|
|
|
18
18
|
private summarizeResult;
|
|
19
19
|
private selectHistoryWindow;
|
|
20
20
|
private authHeaders;
|
|
21
|
+
private buildQueryResponse;
|
|
21
22
|
initRegistry(): Promise<string>;
|
|
22
23
|
private resolveRequiredFilters;
|
|
23
24
|
private castRequiredValue;
|
|
@@ -29,6 +30,7 @@ declare class AiClient {
|
|
|
29
30
|
private applyRequiredFilters;
|
|
30
31
|
private executeToolCall;
|
|
31
32
|
query(request: QueryRequest): Promise<QueryResponse>;
|
|
33
|
+
queryStream(request: QueryRequest, onEvent?: (event: StreamEvent) => void): Promise<QueryResponse>;
|
|
32
34
|
getConversationMessages(conversationId: string): Promise<ConversationMessage[]>;
|
|
33
35
|
}
|
|
34
36
|
export declare function createAiClient(config: ClientConfig): AiClient;
|