@rpcbase/server 0.375.0 → 0.377.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/boot/server.js +6 -3
- package/openai.js +2 -2
- package/package.json +1 -1
- package/queue/dispatch_indexer_queue.js +17 -8
- package/rts/index.js +31 -9
- package/search/constants.ts +1 -0
- package/search/ensure_index.ts +24 -8
- package/search/get_client.ts +1 -1
- package/search/index.ts +1 -0
- package/src/access-control/apply_policies.js +0 -1
- package/src/models/SearchHistory.ts +22 -0
- package/src/models/index.js +1 -0
package/boot/server.js
CHANGED
|
@@ -6,19 +6,22 @@ const rb_server_plugin = require("rb-plugin-server")
|
|
|
6
6
|
|
|
7
7
|
const http = require("http")
|
|
8
8
|
|
|
9
|
-
const {
|
|
9
|
+
const {client_router, express} = require("../")
|
|
10
|
+
|
|
11
|
+
const {init_rts} = require("../rts")
|
|
10
12
|
|
|
11
|
-
const realtime_state = require("../rts")
|
|
12
13
|
|
|
13
14
|
const {SERVER_PORT} = process.env
|
|
14
15
|
|
|
15
16
|
const app = express()
|
|
16
17
|
const server = http.createServer(app)
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
init_rts(server)
|
|
19
20
|
|
|
20
21
|
rb_server_plugin.default(app)
|
|
21
22
|
|
|
23
|
+
client_router(app)
|
|
24
|
+
|
|
22
25
|
server.listen(SERVER_PORT, "0.0.0.0", () => {
|
|
23
26
|
console.log(`listening on 0.0.0.0:${SERVER_PORT}`)
|
|
24
27
|
})
|
package/openai.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const OpenAI = require("openai")
|
|
2
2
|
|
|
3
|
+
|
|
3
4
|
const {OPENAI_API_KEY} = process.env
|
|
4
5
|
|
|
5
6
|
export const openai = new OpenAI({
|
|
6
7
|
apiKey: OPENAI_API_KEY,
|
|
7
8
|
})
|
|
8
9
|
|
|
9
|
-
export const EMBEDDINGS_MODEL_NAME = "text-embedding-3-
|
|
10
|
-
export const EMBEDDINGS_VECTOR_LENGTH = 1536
|
|
10
|
+
export const EMBEDDINGS_MODEL_NAME = "text-embedding-3-large"
|
package/package.json
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
|
|
3
|
-
const dispatch_indexer_queue = (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
const dispatch_indexer_queue = (
|
|
4
|
+
queue,
|
|
5
|
+
model_name,
|
|
6
|
+
coll_name,
|
|
7
|
+
op,
|
|
8
|
+
doc,
|
|
9
|
+
update_description,
|
|
10
|
+
) => {
|
|
11
|
+
queue.add(
|
|
12
|
+
"index_item",
|
|
13
|
+
{op, doc, model_name, coll_name, update_description},
|
|
14
|
+
{
|
|
15
|
+
jobId: `index-item-${op}-${doc._id}-${Date.now()}`,
|
|
16
|
+
removeOnComplete: true,
|
|
17
|
+
removeOnFail: true,
|
|
18
|
+
},
|
|
19
|
+
)
|
|
10
20
|
}
|
|
11
21
|
|
|
12
|
-
|
|
13
22
|
module.exports = dispatch_indexer_queue
|
package/rts/index.js
CHANGED
|
@@ -24,8 +24,8 @@ const QUERY_MAX_LIMIT = 4096
|
|
|
24
24
|
// TODO: does this even support multiple clients ?
|
|
25
25
|
// TODO: investigate multiple change streams event triggers per client
|
|
26
26
|
// TODO: add tracing
|
|
27
|
-
|
|
28
27
|
const _sockets = {}
|
|
28
|
+
const _handlers = []
|
|
29
29
|
const _change_streams = {}
|
|
30
30
|
// we index both queries and socket queries
|
|
31
31
|
// to avoid going through all queries when a socket disconnects
|
|
@@ -87,7 +87,7 @@ const dispatch_doc_change = (model, doc) => {
|
|
|
87
87
|
// socket was destroyed
|
|
88
88
|
// TODO: remove socket id from _queries when destroyed
|
|
89
89
|
if (!s) {
|
|
90
|
-
log("NO SOCKET RETURNING")
|
|
90
|
+
log("NO SOCKET, RETURNING")
|
|
91
91
|
return
|
|
92
92
|
}
|
|
93
93
|
// console.log("dipatch change", query_key)
|
|
@@ -211,7 +211,7 @@ const add_change_stream = (socket_id, {model_name, query, query_key, options}) =
|
|
|
211
211
|
try {
|
|
212
212
|
model = mongoose.model(model_name)
|
|
213
213
|
} catch (err) {
|
|
214
|
-
console.error("ERROR registering model:", model_name)
|
|
214
|
+
console.error("add change stream:ERROR registering model:", model_name)
|
|
215
215
|
console.error(err)
|
|
216
216
|
return
|
|
217
217
|
}
|
|
@@ -261,6 +261,13 @@ const add_change_stream = (socket_id, {model_name, query, query_key, options}) =
|
|
|
261
261
|
|
|
262
262
|
|
|
263
263
|
const run_query = async(socket_id, {model_name, query, query_key, options}) => {
|
|
264
|
+
// Check if the model is registered
|
|
265
|
+
if (!mongoose.models[model_name]) {
|
|
266
|
+
// throw new Error(`Model "${model_name}" is not registered.`)
|
|
267
|
+
console.error(colors.red("ERROR: model not registered"), model_name, "will skip query")
|
|
268
|
+
return
|
|
269
|
+
}
|
|
270
|
+
|
|
264
271
|
const model = mongoose.model(model_name)
|
|
265
272
|
|
|
266
273
|
log("run_query", {model_name, query, query_key, options})
|
|
@@ -278,7 +285,7 @@ const run_query = async(socket_id, {model_name, query, query_key, options}) => {
|
|
|
278
285
|
}
|
|
279
286
|
|
|
280
287
|
const query_options = get_query_options(ctx, options)
|
|
281
|
-
|
|
288
|
+
// log("QUERY", query)
|
|
282
289
|
const query_promise = model.find(query, options.projection, query_options)
|
|
283
290
|
|
|
284
291
|
if (query_options.sort) {
|
|
@@ -351,7 +358,7 @@ const disconnect_handler = (socket_id, reason) => {
|
|
|
351
358
|
}
|
|
352
359
|
|
|
353
360
|
|
|
354
|
-
const
|
|
361
|
+
const init_rts = async(server) => {
|
|
355
362
|
// register the delete plugin
|
|
356
363
|
mongoose.plugin(mongoose_delete_plugin)
|
|
357
364
|
// txn plugin
|
|
@@ -395,9 +402,11 @@ const rts_server = async(server) => {
|
|
|
395
402
|
|
|
396
403
|
io.on("connection", (socket) => {
|
|
397
404
|
_sockets[socket.id] = socket
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
405
|
+
|
|
406
|
+
log("socket connected", socket.id)
|
|
407
|
+
|
|
408
|
+
// RTS handlers
|
|
409
|
+
socket.on("registerQuery", (payload) => {
|
|
401
410
|
add_change_stream(socket.id, payload)
|
|
402
411
|
})
|
|
403
412
|
|
|
@@ -412,12 +421,25 @@ const rts_server = async(server) => {
|
|
|
412
421
|
console.log("run_query caught error", err)
|
|
413
422
|
}
|
|
414
423
|
})
|
|
424
|
+
// !RTS handlers
|
|
425
|
+
|
|
426
|
+
// register_rts_handler: custom server generic handlers
|
|
427
|
+
const cleanup_fns = _handlers.map((handler_fn) => handler_fn(socket))
|
|
415
428
|
|
|
416
429
|
socket.on("disconnect", (reason) => {
|
|
417
430
|
disconnect_handler(socket.id, reason)
|
|
431
|
+
cleanup_fns
|
|
432
|
+
.filter((fn) => typeof fn === "function")
|
|
433
|
+
.forEach((fn) => fn())
|
|
418
434
|
})
|
|
419
435
|
})
|
|
420
436
|
}
|
|
421
437
|
|
|
422
438
|
|
|
423
|
-
|
|
439
|
+
const register_rts_handler = (handler_fn) => {
|
|
440
|
+
console.log("REGister handler", handler_fn)
|
|
441
|
+
_handlers.push(handler_fn)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
module.exports = {init_rts, register_rts_handler}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_SEARCH_EMBEDDINGS_DIMENSIONS = 2048
|
package/search/ensure_index.ts
CHANGED
|
@@ -6,26 +6,35 @@ const log = debug("rb:search:ensure_index")
|
|
|
6
6
|
|
|
7
7
|
export const ensure_index = async({
|
|
8
8
|
index_id,
|
|
9
|
-
|
|
9
|
+
mappings,
|
|
10
10
|
}: {
|
|
11
|
-
index_id: string
|
|
12
|
-
|
|
11
|
+
index_id: string;
|
|
12
|
+
mappings: any;
|
|
13
13
|
}) => {
|
|
14
14
|
const client = get_client()
|
|
15
15
|
|
|
16
16
|
try {
|
|
17
17
|
const exists = await client.indices.exists({index: index_id})
|
|
18
|
-
|
|
18
|
+
|
|
19
|
+
if (exists) {
|
|
20
|
+
await client.indices.putMapping({
|
|
21
|
+
index: index_id,
|
|
22
|
+
body: mappings,
|
|
23
|
+
})
|
|
24
|
+
log("updated index mappings", index_id)
|
|
25
|
+
return
|
|
26
|
+
}
|
|
19
27
|
} catch (err) {
|
|
20
|
-
console.log(
|
|
28
|
+
console.log(
|
|
29
|
+
"Error checking for index existance",
|
|
30
|
+
JSON.stringify(err, null, 2),
|
|
31
|
+
)
|
|
21
32
|
}
|
|
22
33
|
|
|
23
34
|
const res = await client.indices.create({
|
|
24
35
|
index: index_id,
|
|
25
36
|
body: {
|
|
26
|
-
mappings
|
|
27
|
-
properties: mappings_properties,
|
|
28
|
-
},
|
|
37
|
+
mappings,
|
|
29
38
|
},
|
|
30
39
|
})
|
|
31
40
|
|
|
@@ -34,4 +43,11 @@ export const ensure_index = async({
|
|
|
34
43
|
|
|
35
44
|
log("created index", index_id)
|
|
36
45
|
}
|
|
46
|
+
|
|
37
47
|
console.log("ensure_index.ts")
|
|
48
|
+
|
|
49
|
+
// {
|
|
50
|
+
// dynamic: true,
|
|
51
|
+
// // date_detection: false,
|
|
52
|
+
// properties: mappings_properties,
|
|
53
|
+
// }
|
package/search/get_client.ts
CHANGED
package/search/index.ts
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import mongoose, {Schema, Model, Document} from "@rpcbase/server/mongoose"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
interface ISearchHistory extends Document {
|
|
5
|
+
server_timestamp_ms: number;
|
|
6
|
+
context: any;
|
|
7
|
+
data: any;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const SearchHistorySchema: Schema<ISearchHistory> = new Schema(
|
|
11
|
+
{
|
|
12
|
+
server_timestamp_ms: Number,
|
|
13
|
+
context: mongoose.Schema.Types.Mixed,
|
|
14
|
+
data: mongoose.Schema.Types.Mixed,
|
|
15
|
+
},
|
|
16
|
+
{timestamps: true},
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
export const SearchHistory: Model<ISearchHistory> = mongoose.model<ISearchHistory>(
|
|
20
|
+
"SearchHistory",
|
|
21
|
+
SearchHistorySchema,
|
|
22
|
+
)
|
package/src/models/index.js
CHANGED