Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions client/src/components/Artifacts/useDebounceCodeBlock.ts

This file was deleted.

15 changes: 8 additions & 7 deletions client/src/hooks/Conversations/useDebouncedInput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debounce from 'lodash/debounce';
import React, { useState, useCallback } from 'react';
import React, { useState, useCallback, useMemo } from 'react';
import type { SetterOrUpdater } from 'recoil';
import type { TSetOption } from '~/common';
import { defaultDebouncedDelay } from '~/common';
Expand Down Expand Up @@ -29,10 +29,10 @@ function useDebouncedInput<T = unknown>({

/** A debounced function to call the passed setOption with the optionKey and new value.
*
Note: We use useCallback to ensure our debounced function is stable across renders. */
const setDebouncedOption = useCallback(
debounce(setOption && optionKey ? setOption(optionKey) : setter, delay),
[],
Note: We use useMemo to ensure our debounced function is stable across renders and properly typed. */
const setDebouncedOption = useMemo(
() => debounce(setOption && optionKey ? setOption(optionKey) : setter || (() => {}), delay),
[setOption, optionKey, setter, delay],
);

/** An onChange handler that updates the local state and the debounced option */
Expand All @@ -42,8 +42,9 @@ function useDebouncedInput<T = unknown>({
typeof e !== 'object'
? e
: ((e as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>).target
.value as unknown as T);
if (numeric === true) {
.value as unknown as T);
// Handle numeric conversion only if value is not undefined and not empty string
if (numeric === true && newValue !== undefined && newValue !== '') {
newValue = Number(newValue) as unknown as T;
}
setValue(newValue);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/data-schemas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@librechat/data-schemas",
"version": "0.0.7",
"version": "0.0.8",
"description": "Mongoose schemas and models for LibreChat",
"type": "module",
"main": "dist/index.cjs",
Expand Down
11 changes: 11 additions & 0 deletions packages/data-schemas/src/models/convo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import type * as t from '~/types';
import mongoMeili from '~/models/plugins/mongoMeili';
import convoSchema from '~/schema/convo';

/**
* Creates or returns the Conversation model using the provided mongoose instance and schema
*/
export function createConversationModel(mongoose: typeof import('mongoose')) {
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
convoSchema.plugin(mongoMeili, {
mongoose,
host: process.env.MEILI_HOST,
apiKey: process.env.MEILI_MASTER_KEY,
/** Note: Will get created automatically if it doesn't exist already */
indexName: 'convos',
primaryKey: 'conversationId',
});
}
return (
mongoose.models.Conversation || mongoose.model<t.IConversation>('Conversation', convoSchema)
);
Expand Down
13 changes: 12 additions & 1 deletion packages/data-schemas/src/models/message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import messageSchema from '~/schema/message';
import type * as t from '~/types';
import mongoMeili from '~/models/plugins/mongoMeili';
import messageSchema from '~/schema/message';

/**
* Creates or returns the Message model using the provided mongoose instance and schema
*/
export function createMessageModel(mongoose: typeof import('mongoose')) {
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
messageSchema.plugin(mongoMeili, {
mongoose,
host: process.env.MEILI_HOST,
apiKey: process.env.MEILI_MASTER_KEY,
indexName: 'messages',
primaryKey: 'messageId',
});
}

return mongoose.models.Message || mongoose.model<t.IMessage>('Message', messageSchema);
}
10 changes: 6 additions & 4 deletions packages/data-schemas/src/models/plugins/mongoMeili.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import _ from 'lodash';
import { MeiliSearch, Index } from 'meilisearch';
import mongoose, { Schema, Document, Model, Query } from 'mongoose';
import type { FilterQuery, Types, Schema, Document, Model, Query } from 'mongoose';
import logger from '~/config/meiliLogger';

interface MongoMeiliOptions {
host: string;
apiKey: string;
indexName: string;
primaryKey: string;
mongoose: typeof import('mongoose');
}

interface MeiliIndexable {
Expand Down Expand Up @@ -314,7 +315,7 @@ const createMeiliMongooseModel = ({
}

await this.collection.updateMany(
{ _id: this._id as mongoose.Types.ObjectId },
{ _id: this._id as Types.ObjectId },
{ $set: { _meiliIndex: true } },
);
}
Expand Down Expand Up @@ -398,6 +399,7 @@ const createMeiliMongooseModel = ({
* @param options.primaryKey - The primary key field for indexing.
*/
export default function mongoMeili(schema: Schema, options: MongoMeiliOptions): void {
const mongoose = options.mongoose;
validateOptions(options);

// Add _meiliIndex field to the schema to track if a document has been indexed in MeiliSearch.
Expand Down Expand Up @@ -452,7 +454,7 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
const convoIndex = client.index('convos');
const deletedConvos = await mongoose
.model('Conversation')
.find(conditions as mongoose.FilterQuery<unknown>)
.find(conditions as FilterQuery<unknown>)
.lean();
const promises = deletedConvos.map((convo: Record<string, unknown>) =>
convoIndex.deleteDocument(convo.conversationId as string),
Expand All @@ -464,7 +466,7 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
const messageIndex = client.index('messages');
const deletedMessages = await mongoose
.model('Message')
.find(conditions as mongoose.FilterQuery<unknown>)
.find(conditions as FilterQuery<unknown>)
.lean();
const promises = deletedMessages.map((message: Record<string, unknown>) =>
messageIndex.deleteDocument(message.messageId as string),
Expand Down
11 changes: 0 additions & 11 deletions packages/data-schemas/src/schema/convo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Schema } from 'mongoose';
import mongoMeili from '~/models/plugins/mongoMeili';
import { conversationPreset } from './defaults';
import { IConversation } from '~/types';

Expand Down Expand Up @@ -48,14 +47,4 @@ convoSchema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 });
convoSchema.index({ createdAt: 1, updatedAt: 1 });
convoSchema.index({ conversationId: 1, user: 1 }, { unique: true });

if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
convoSchema.plugin(mongoMeili, {
host: process.env.MEILI_HOST,
apiKey: process.env.MEILI_MASTER_KEY,
/** Note: Will get created automatically if it doesn't exist already */
indexName: 'convos',
primaryKey: 'conversationId',
});
}

export default convoSchema;
10 changes: 0 additions & 10 deletions packages/data-schemas/src/schema/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import mongoose, { Schema } from 'mongoose';
import type { IMessage } from '~/types/message';
import mongoMeili from '~/models/plugins/mongoMeili';

const messageSchema: Schema<IMessage> = new Schema(
{
Expand Down Expand Up @@ -166,13 +165,4 @@ messageSchema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 });
messageSchema.index({ createdAt: 1 });
messageSchema.index({ messageId: 1, user: 1 }, { unique: true });

if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
messageSchema.plugin(mongoMeili, {
host: process.env.MEILI_HOST,
apiKey: process.env.MEILI_MASTER_KEY,
indexName: 'messages',
primaryKey: 'messageId',
});
}

export default messageSchema;